0

I have a code as below. Whenever temps = $Nodes loop should end. I checked temps every in iteration and saw that temps = $Nodes once as expected but the loop was not ended. This code worked for VS10 but not working for linux. Any idea?

std::string temps;
s_mesh_file.open (mesh_file.c_str());

do
{
    getline (s_mesh_file, temps);
    std::cout << "temps: " << temps << std::endl;
    std::cin.ignore();
}
while (temps != "$Nodes");
Shibli
  • 5,879
  • 13
  • 62
  • 126
  • 3
    Maybe `temps` contains some trailing white spaces at the end? So you see "`$Nodes`" on your window but it's actually "`$Nodes `"? – Andy Prowl Jan 10 '13 at 20:52
  • 4
    Are they using the same exact input file? Including with Windows newlines? Linux doesn't like windows newlines. (Nor does Windows like Linux's) @AndyProwl it's probably `"$Nodes\r"` – Mooing Duck Jan 10 '13 at 20:53
  • 3
    I guess this is your answer: http://stackoverflow.com/a/8960119/112968 – knittl Jan 10 '13 at 20:53
  • 2
    Have you checked that the string is **exactly** as expected (i.e. no invisible characters like spaces at the end or similar changes?) – David Rodríguez - dribeas Jan 10 '13 at 20:53
  • 2
    try `std::cout << "temps: '" << temps << "'" << std::endl;` and check for additional whitespaces – higuaro Jan 10 '13 at 20:54
  • 2
    @h3nr1x, Good start, but no invisible characters. Print the length as well. – chris Jan 10 '13 at 20:55
  • @MooingDuck: Aha! That was the problem. I created a new file and copy pasted the content again instead of copying the file itself. Thank you! – Shibli Jan 10 '13 at 20:56

2 Answers2

1

Are they using the same exact input file? Including with Windows newlines? Linux doesn't like windows newlines. (Nor does Windows like Linux's)

In windows, each newline is a \r followed by an \n. In Linux, a newline is just \n. Streams open by default in "text mode" which converts whatever the system newline is into just \n. So, when you read the file in Windows, it's dropping the \r helpfully. However, Linux doesn't realize it's part of the newline, and so is putting the \r onto the end of your string like any other normal character, which makes it not match the hardcoded "$Nodes".

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
0

Remove std::cin.ignore();. You are waiting for user input actually. Tested on linux.

hate-engine
  • 2,300
  • 18
  • 26
  • 1
    You must've missed the comments. I didn't even notice that was for `cin` until a short while ago, so I was confused why the OP was getting correct text. I strongly believe the OP is pressing enter on each iteration. – chris Jan 10 '13 at 21:01