-
August 12th, 2012, 07:15 PM
#1
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;
)
-
August 12th, 2012, 08:12 PM
#2
Re: Correct syntax for for using getline with vectors?
Originally Posted by Ulnarian
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.
-
August 15th, 2012, 05:06 AM
#3
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.
-
August 15th, 2012, 05:27 AM
#4
Re: Correct syntax for for using getline with vectors?
Originally Posted by treuss
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);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|