cout every line in a csv file?

So if we have a file that contains 5 elements each line

3 4 5 6 NY
6 7 8 9 NJ
1 2 3 4 WY
4 3 2 1 NC

I would write it as

while(!streamname.eof()){
streamname >> a >> b >> c >> d >> st;
cout << a << "" << b << "" << c << "" << d << "" << st << endl;
}

so that outcome can look like

3 4 5 6 NY
6 7 8 9 NJ
1 2 3 4 WY
4 3 2 1 NC

But it's not working and I'm stuck
So unless the formating of the msg above is wrong somewhere, you want the output to look just like the input.
The only thing I see is your not putting a space between each value.

1
2
3
4
5
while(!streamname.eof())
{
streamname >> a >> b >> c >> d >> st; 
cout << a << " " << b << " " << c << " " << d << " " << st << endl; 
}
1) That's not a Comma Separated Value file. It's a space separated file.

2) Do not loop on ! stream.eof(). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> operation as the condition in the while statement.
1
2
3
  while (stream >> var) 
  {  //  Good operation
  }


3) You haven't shown the declarations of a,b,c,d or st. So don;t know if that statement is correct.

4) PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Wikipedia:
In addition, the term "CSV" also denotes some closely related delimiter-separated formats that use different field delimiters.

It probably does not help the misuse that applications like MS Excel show the same "tick all delimiters that apply" dialog, no matter what ascii file you open.
Topic archived. No new replies allowed.