I’m coding a class that should read OFF Files and I’m having the following issue:
If I compile it within the Code::Blocks environment everything works fine.
If the first line of the file that is to be loaded differs from “OFF” it will jump into the second if-statement and exit the program…
However if I compile with g++ in cygwin the program jumps into the second if-Statement no matter what is actually written in the file.
Any Suggestions?
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
polyeder offManager::readOFF(std::string filename)
{
//Open File
std::ifstream file(filename.c_str());
// If file couldn't be opened
if( !file.is_open() )
{
std::cerr << "ERROR: Konnte Datei \""
<< filename << "\" nicht oeffnen!"
<< std::endl;
exit (2);
}
// Test if the file really is an OFF File
std::string line;
std::string off ("OFF");
getline( file, line );
if ( line.compare(off) != 0 )
{
std::cerr << "ERROR: Datei \""
<< filename << "\" ist nicht im OFF Format!"
<< std::endl;
file.close();
exit (2);
}
...
}
If I type g++ -v in cygwin I get the following:
Blablabla
Thread-Modell: posix
gcc-Version 4.5.3 (GCC)
Code::Blocks uses this version:
Thread model: win32
gcc version 4.4.1 (TDM-2 mingw32)
The file you are opening is likely in DOS text format, so it’s lines end with
\r\n, rather than just\n.The Cygwin FAQ has an entry about this issue. I copied your code into a simple
maindriver, and compiled it two ways. First, the regular way:And then, with the suggested fix (open with text translation mode, write with binary mode):