Reputation: 1025
I am reading csv files with javascript and send them to a C++ webserver/backend to upload the files to a server. This file is sent as a string via a HTTP POST request to the webserver.
In javascript the files are properly read and sent to C++ with \r\n
line endings.
When putting the string into a newly created .csv file on the server with
std::string data = // the parsed http body string
std::ofstream file;
file.open(UPLOAD_DIR + "\\" + filename);
file << data;
file.close();
The file is created and the string is put into it but with empty lines after every linebreak. The string still has \r\n
line endings when debugging in C++.
I tried to replace the \r\n
endings with just \n
but the empty lines where still there. Removing the line breaks completely obviously just writes the string into the file without any newlines whatsoever.
How do I put the string into the file without empty lines?
Ps: The csv string looks like this in the C++ debugger:
EDIT Here is a bit of the string:
Time [s];Temperature [C]\r\n0;1150\r\n0.1;1150\r\n0.2;1150\r\n0.3;1150\r\n0.4;1150\r\n0.5;1150\r\n0.6;1150\r\n0.7;1150\r\n0.8;1150\r\n0.9;1150\r\n1;1150\r\n1.1;1150\r\n1.2;1150\r\n1.3;1150\r\n1.4;1150\r\n1.5;1150\r\n1.6;1150\r\n1.7;11
(I know, CSV with semicolons is not RFC conform)
Upvotes: 0
Views: 546
Reputation: 886
I tried to recreate your file write by writing
std::ofstream file("mine.txt");
file << "123\r\nabc\r\n890";
And it did indeed give two line breaks.
Writing just
file << "123\rabc\r890";
did the trick, as well as
file << "123\nabc\n890";
So, it appears although Windows uses both \r\n for line feeds,
just one of them is enough for text files.
Interestingly, when I write
file << "123 \rabc \r \n890";
and do
std::cout << arr[i] << '\t' << int(arr[i]) << '\n';
in a loop,
I got:
1 49
2 50
3 51
32
13
a 97
b 98
c 99
32
13
32
10
8 56
9 57
0 48
(Notice the blank line before 10.)
Windows is strange. ;)
EDIT: Reading the OP's answer, I would also suggest storing them as binary files,
if all you are doing is storing and retrieving the files through the internet.
Upvotes: 0
Reputation: 148965
Well the problem may not be where you think it is.
The file is created and the string is put into it but with empty lines after every linebreak.
No the file is a perfectly nice CSV file. Most implementations (even those allowing different delimiters and quoting characters) agree on the line separator being a 2 characters "\r\n"
.
So the problem only comes from the tool you are using to display the file or the way you try to read it.
This is only my opinion, but I strongly advise you against removing the '\r'
characters from a csv file.
I think that the correct way would be to process the file as binary. That way you would not rely on the OS interpretation of how a '\n'
should be converted when writing it to a file.
Upvotes: 2
Reputation: 1025
The comments provided solved it:
Instead of replacing every occurrence of \r\n
with just \n
I am now just deleting every \r
from the string - and that solves my problem.
Upvotes: 0