Hello!
I have a if statement that is working in windows but does not work in ubuntu.
I have tried putting a cout statement above my while loop to test it was working.It did not output so I suspected that my if statement was a problem.
What's the type of filename? What's the behavior in each platform? Where does the value of filename come from? Did you try printing the value before reaching the if?
I mean, did you try printing the value of filename before reaching the if? Perhaps something is happening that causes it to have a different value in each platform.
ne555 had figured it out!
Add this before the if (or right after you read the filename in from the text file):
1 2 3
size_t pos = filename.find_first_of("\r");
if (pos != filename.npos)
filename.resize(pos);
You must have written the file in Windows, which for text files ends lines with a '\r' followed by '\n'. On input in text mode, Windows removes the '\r' so you don't see it. But Unix just uses '\n' for a line ending, so if there is a '\r' before the '\n' it leaves the '\r' at the end of the string.
so i tried cout << filename <<"'" << endl; , while windows returned me citylocation.txt '
linux returned me ' itylocation.txt
On Windows, it removed the '\r' so the single-quote is printed after the string.
But in Linux, the '\r' caused the terminal to go back to the beginning of the line and print the single-quote.
Ok,with your code to take away the \r seem to got the If statement working , but it seems that the while loop is not working now. I placed a cout statement before and after the while loop, but only the one before is displayed.
Even if the output pointer is returned to the beginning of the line it will still write the data even if that includes overwriting existing data.. Right? Then OP would have noticed I feel.