question on loading a string from a file into a vector

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gdarian216
    New Member
    • Oct 2006
    • 57

    question on loading a string from a file into a vector

    Okay I am writting a program that will take input from a file and store it in a string. Then the string is separated at the whitespaces and these separate strings are loaded in a vector. My problem is that how would I index the vector so that the strings I input can be put in a class?

    okay I have a .h file that looks like this......

    Code:
    // struct, all members are public.
    struct Date
    {
        Date()
          : year(0) {};
    
        string  day;
        string  month;
        int     year;
    };
    
        // struct, all members are public.
    struct Time
    {
        Time()
          : hour(0), minute(0), second(0) {};
    
        int  hour;
        int  minute;
        int  second;
    };
    
        // Only the Log_Entry constructor is not implemented.
    class Log_Entry
    {
      public:
        Log_Entry(string);
    
        string  get_host()            const { return _host; }
        Date    get_date()            const { return _date; }
        Time    get_time()            const { return _time; }
        string  get_request()         const { return _request; }
        string  get_status()          const { return _status; }
        int     get_number_of_bytes() const { return _number_of_bytes; }
    
      private:
        string  _host;
        Date    _date;
        Time    _time;
        string  _request;
        string  _status;
        int     _number_of_bytes;
    };
    
    std::vector<string> split (const string&, char);
    
    std::vector<Log_Entry> parse(string);
    void output_all(const std::vector<Log_Entry> &);
    int  byte_count(const std::vector<Log_Entry> &);
    void output_hosts(std::vector<Log_Entry>);
    
    #endif
    i also have a .cpp file that looks like this

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    using namespace std;
    
    int main () {
      string line;
      ifstream myfile ("example.txt");
      if (myfile.is_open())
      {
        while (! myfile.eof() )
        {
          getline (myfile,line);
    
          cout << line << endl;
        }
        myfile.close();
      }
    
      else cout << "Unable to open file";
    
      return 0;
    }
    this .cpp file will open a txt file and input lines on info one at a time. I am now trying to split the string up and load it in a vector.
  • RRick
    Recognized Expert Contributor
    • Feb 2007
    • 463

    #2
    Vector has a push_back method that puts an entry at the end of the vector. With this you don't need to worry about indexes or making sure the vector has enough space allocated.

    You can also skip the getline if you use myfile to fill strings with >>. With strings, the ifstream skips over whitespace automatically. In this case, whitespace includes spaces, tabs and '\n'.

    Comment

    Working...