Getline and CSV file

So I have been trying to extract each line from a CSV file in the section of code below and I am having trouble with it.

1
2
3
4
5
6
7
8
9
10
11
12

        std::ifstream Stream;
	Stream.open("spam.csv");
        int count = 0;
	while ((getline(Stream, y) && (count < 5)))
	{
		getline(Stream, y);
		std::cout << "\n " << y;
		std::cout << "\n-------------------------";
		count++;
	}


This code works fine in extracting the lines in the CSV. However, the only problem with it is that it skips a line every time it iterates.


It is supposed to print out the first five lines of this CSV file

ham,"Go until jurong point, crazy.. Available only in bugis n great world la e buffet... Cine there got amore wat...",,,
ham,Ok lar... Joking wif u oni...,,,
spam,Free entry in 2 a wkly comp to win FA Cup final tkts 21st May 2005. Text FA to 87121 to receive entry question(std txt rate)T&C's apply 08452810075over18's,,,
ham,U dun say so early hor... U c already then say...,,,
ham,"Nah I don't think he goes to usf, he lives around here though",,,
spam,"FreeMsg Hey there darling it's been 3 week's now and no word back! I'd like some fun you up for it still? Tb ok! XxX std chgs to send, å£1.50 to rcv",,,
ham,Even my brother is not like to speak with me. They treat me like aids patent.,,,
ham,As per your request 'Melle Melle (Oru Minnaminunginte Nurungu Vettam)' has been set as your callertune for all Callers. Press *9 to copy your friends Callertune,,,
spam,WINNER!! As a valued network customer you have been selected to receivea å£900 prize reward! To claim call 09061701461. Claim code KL341. Valid 12 hours only.,,,



But instead, it ends up printing the following:



 ham,"Go until jurong point, crazy.. Available only in bugis n great world la e buffet... Cine there got amore wat...",,,
-------------------------
 spam,Free entry in 2 a wkly comp to win FA Cup final tkts 21st May 2005. Text FA to 87121 to receive entry question(std txt rate)T&C's apply 08452810075over18's,,,
-------------------------
 ham,"Nah I don't think he goes to usf, he lives around here though",,,
-------------------------
 ham,Even my brother is not like to speak with me. They treat me like aids patent.,,,
-------------------------
 spam,WINNER!! As a valued network customer you have been selected to receivea σú900 prize reward! To claim call 09061701461. Claim code KL341. Valid 12 hours only.,,,
------------------[\code]-------



I am running this project in Visual Studio 2017. The rest of the code runs fine. This is the only part that is giving me trouble.

I have looked for the getline function definition in C++ reference sites, but I really don't understand why it is not giving the expected output. Could anyone help?

PS. I am new to this Forum, so please forgive me if I failed to follow any of the rules.

Last edited on
You're reading the file twice once on line 5 (the only one you need) and once on line 7 (which is not needed).

Is there a reason you're only reading 5 lines?

It's an arbitrary amount of lines I chose, but the problem is that I want it to go line-by-line instead of skipping one line at a time.
Just remove line 7 in your current code snippet.

OK. Thank you so much. I did not know it was that easy! I guess I should've messed around with it more before asking dumb questions like that.
Topic archived. No new replies allowed.