Hi everybody,
I'm creating a program which reads from a csv file and stores each column in an array of structs. Anyway, in Linux with Kdevelop everything works fine. My problem is that I want to use it also in windows. I've compiled the code with code::blocks in windows, and at first glance there's no problem, except for de text encoding. As I'm trying to create a german training programm, I must assure input and output cp works correctly.
First, I use SetConsoleCP (1252) and SetConsoleOutputCP (1252), everything great.
The problem come here:
First, I saved the csv file in Linux with windows-1252 encoding. Works perfectly with windows, and my program is completely usable, except that when I open the .csv file with notepad, it appears without each newline.
After some searching, I notice the \r\n (windows) vrs \n (linux) issue. I understand that the newline sequence is still there, but notepad needs \r\n in order to read it properly (wordpad opens it correctly, though).
To get the file completely "windowsed", I open the file with libreoffice, and I save it again. Now with notepad each line shows correctly, but for my surprise, My program doesn't run properly anymore!
It seems that the program works fine with only \n, but it fails with \r\n.
to check where is the mistake, I've put a simple loop at the beginning of the program:
1 2 3 4 5 6 7 8 9 10 11
|
fstream diccionari ("diccionari.csv");
string linia;
for (i=0; i<n; i++) // n has been previously assigned with the total amount of entries
{
ID [i] = diccionari.tellg (); //ID [] is an array with each newline position
getline (diccionari, linia);
cout <<linia <<endl; //it outputs the first line correctly, but not the others.
}
cout <<ID[1]; // Just to check, but it doesn't point correctly, actually it points to where the getline WRONGLY starts getting the second line
|
As I say in the comment, it seems like the tellg pointer doesn't point to the new line, but elsewhere. (actually, it points to the third line, at the same point where the second line would finish, and of course there are lines empty, as they are shorter than the previous --> this makes me think with some issue with carriage return...?)
I'm really stuck, and I repeat, it works perfectly with the .csv encoded form linux OR FORM NOTEPAD (ANSI) without newlines showing. The problem is when I try to have the file with newlines showing in notepad.
Thanks!