if statement working in windows but not in ubuntu

Jan 27, 2019 at 6:14pm
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  if(filename == "citylocation.txt")
	{
		while (getline(inputFile, aLine))
		{
			cityCordsX = stoi(aLine.substr(1, ','));
			cityCordsY = stoi(aLine.substr(4, '-'));
			cityID = stoi(aLine.substr(7, '-'));
			cityName = aLine.substr(9, '\n');



			cout << cityCordsX << "," << cityCordsY << "," << cityID << "," << cityName << endl;

		}
	}
Jan 27, 2019 at 6:16pm
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?
Jan 27, 2019 at 6:36pm
filename type is string , the value of filename comes from a txt file.

On windows the while loop executes and this line : cout << cityCordsX << "," << cityCordsY << "," << cityID << "," << cityName << endl is printed out.

Did you try printing the value

What value are you referring too?

I apologise in advance as the next reply will be late as it's very late here...
Jan 27, 2019 at 9:44pm
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.
Jan 27, 2019 at 11:05pm
Tried to cout << filename << endl; , both platform returned me citylocation.txt

so i tried cout << filename <<"'" << endl; , while windows returned me citylocation.txt '
linux returned me ' itylocation.txt
Jan 27, 2019 at 11:13pm
that's why you shouldn't print but watch them in the debugger
the value was probably "citylocation.txt\r"
Jan 27, 2019 at 11:27pm
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.
Last edited on Jan 27, 2019 at 11:34pm
Jan 28, 2019 at 7:34am
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.

1
2
3
4
5
6
if(filename == "citylocation.txt")
	{
                cout << "test" << endl;
		while (getline(inputFile, aLine))
		{
                  cout << "test2" << endl;
Jan 28, 2019 at 9:36am
You probably have got a '\r' at the end of every line.
Jan 28, 2019 at 10:51am
I'll repeat, use the debugger.
Jan 28, 2019 at 11:03am
You probably have got a '\r' at the end of every line.


How would that make getline fail though? .-.
Jan 28, 2019 at 11:31am
I don't think it would make it fail, but it could screw up the output.
Jan 28, 2019 at 11:47am
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.
Jan 28, 2019 at 11:53am
If he outputs something after it could overwrite the previous output which makes it look like it never happened.
Topic archived. No new replies allowed.