I have this code which is supposed to cout in console the information from the .csv file;
while(file.good())
{
getline(file, ID, ',');
cout << "ID: " << ID << " " ;
getline(file, nome, ',') ;
cout << "User: " << nome << " " ;
getline(file, idade, ',') ;
cout << "Idade: " << idade << " " ;
getline(file, genero, ' ') ;
cout << "Sexo: " << genero<< " " ;
}
And a csv file that has this (when I open with notepad):
0,Filipe,19,M
1,Maria,20,F
2,Walter,60,M
Whenever I run the program the console will display this:
My question is why isn't the program repeating those cout messages in every line instead of only in the first one
Btw , nome is name, idade is age, and genero/sexo is gender, forgot to translate before creating this post
Open CSV File using File Pointer in append mode which will place a pointer to the end of the file. Take Input from the user in temporary variables. Use fprintf() and separate variables according to their order and comma.
To read a CSV file, We will open the file using ' fstream ' or ' ifstream ' C++ library. Then, we will read the file line by line using the getline() method as each line ends with a newline character. The getline() method takes a file stream as its first input argument and a string variable as its second argument.
Spark Read CSV using Multiline Option In order to process the CSV file with values in rows scattered across multiple lines, use option("multiLine",true) .
A CSV is a comma-separated values file, which allows data to be saved in a tabular format. CSVs look like a garden-variety spreadsheet but with a . csv extension. CSV files can be used with most any spreadsheet program, such as Microsoft Excel or Google Spreadsheets.
You can follow this answer to see many different ways to process CSV in C++.
In your case, the last call to getline
is actually putting the last field of the first line and then all of the remaining lines into the variable genero
. This is because there is no space delimiter found up until the end of file. Try changing the space character into a newline instead:
getline(file, genero, file.widen('\n'));
or more succinctly:
getline(file, genero);
In addition, your check for file.good()
is premature. The last newline in the file is still in the input stream until it gets discarded by the next getline()
call for ID
. It is at this point that the end of file is detected, so the check should be based on that. You can fix this by changing your while
test to be based on the getline()
call for ID
itself (assuming each line is well formed).
while (getline(file, ID, ',')) {
cout << "ID: " << ID << " " ;
getline(file, nome, ',') ;
cout << "User: " << nome << " " ;
getline(file, idade, ',') ;
cout << "Idade: " << idade << " " ;
getline(file, genero);
cout << "Sexo: " << genero<< " " ;
}
For better error checking, you should check the result of each call to getline()
.
a csv-file is just like any other file a stream of characters. the getline reads from the file up to a delimiter however in your case the delimiter for the last item is not ' ' as you assume
getline(file, genero, ' ') ;
it is newline \n
so change that line to
getline(file, genero); // \n is default delimiter
Your csv is malformed. The output is not three loopings but just one output. To ensure that this is a single loop, add a counter and increment it with every loop. It should only count to one.
This is what your code sees
0,Filipe,19,M\n1,Maria,20,F\n2,Walter,60,M
Try this
0,Filipe,19,M
1,Maria,20,F
2,Walter,60,M
while(file.good())
{
getline(file, ID, ',');
cout << "ID: " << ID << " " ;
getline(file, nome, ',') ;
cout << "User: " << nome << " " ;
getline(file, idade, ',') ;
cout << "Idade: " << idade << " " ;
getline(file, genero) ; \\ diff
cout << "Sexo: " << genero;\\diff
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With