Correct syntax for for using getline with vectors?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Correct syntax for for using getline with vectors?

  1. #1
    Join Date
    May 2006
    Posts
    102

    Correct syntax for for using getline with vectors?

    I'm trying to get my program to read a series of comma delimited values from a file into a vector. However, I am unsure how to actually go about doing this. I've posted my best guess below but it's really just a stab in the dark and I always receive a compiler error. Does anyone know the correct way to go about this?

    Thanks!


    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    vector <string> v_input;
    
    int main()
    
    {
        ofstream fout ("Insert File Path Here.txt");
        fout << "4, 8, 15, 16, 23, 42";
        fout.close();
        
        ifstream getdata ("Insert File Path Here.txt");
        
        while(!getdata.eof())
        getline(getdata, v_input.push_back(), ','); //read comma separated values into a vector--problem line!
        
        ifstream.close();
        
        return 0;
    )

  2. #2
    Join Date
    Apr 1999
    Posts
    27,424

    Re: Correct syntax for for using getline with vectors?

    Quote Originally Posted by Ulnarian View Post
    I'm trying to get my program to read a series of comma delimited values from a file into a vector. However, I am unsure how to actually go about doing this. I've posted my best guess below but it's really just a stab in the dark and I always receive a compiler error. Does anyone know the correct way to go about this?
    Extracting comma-separated values is not as trivial as your code is suggesting. If it were as simple as the code you have written, there would be no need (or very little need) for things such as tokenization libraries (such as boost) -- all you would need would be to use getline() with a delimiter and push_back(), and unfortunately, that won't work.

    Read the entire line into a string and parse the string yourself. This requires you to write a loop (using functions such as find_first_of() and similar functions). When a value is detected, push it onto the vector. Or you can use the boost tokenization library if you don't want to write your own loop. Those are just two suggestions out of the many that exist.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; August 12th, 2012 at 08:15 PM.

  3. #3
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Correct syntax for for using getline with vectors?

    Correct syntax would be something like:
    Code:
        while(! getdata.eof()) {
            string s;
            getline(getdata, s, ',');
            v_input.push_back(s);
        }
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,261

    Re: Correct syntax for for using getline with vectors?

    Quote Originally Posted by treuss View Post
    Correct syntax would be something like:
    Code:
        while(! getdata.eof()) {
            string s;
            getline(getdata, s, ',');
            v_input.push_back(s);
        }
    Instead of using eof() to control the loop like that, I recommend:
    Code:
    string s;
    while (getline(getdata, s, ',')) {
        v_input.push_back(s);
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center