I'm writing a C++ program that parses XML into JSON for a class and it works great when I compile in Visual Studio but behaves strangely when compiled with g++ in Linux.
With a bit of testing, I believe I have tracked the issue down into a difference in the way new lines are handled between the different compilers, here's some of the code I'm using to debug:
while (!fileToRead.eof()) { //Until we have reached the end of the file: ...
cout << endl << "newloop: ";
char c;
fileToRead.get(c);
cout << "read " << c << " ";
if (c != '\n' && c != '\t')
cout << "is a text character.";
}
When I run an executable created in Visual Studio, it outputs the following for new line characters it reads:
newloop: read
newloop: read
newloop: read
newloop: read
When I run it on Linux when compiled with g++, it outputs the following for new line characters it reads:
is a text character.
newloop: read
is a text character.
newloop: read
is a text character.
newloop: read
newloop: read
As you can see, when compiled with g++ there are 2 problems:
- The third cout ("is a text character.") runs before the first and seconds couts ('"endl << << "newloop: "' and '"read " << c << "')
- The if statement ("if (c != '\n' && c != '\t')") runs even when c is a new line character.
Can anyone explain what's going on here?