views:

69

answers:

2

Hi,

I am writing a program that reads a file, whose first two lines are:

Field of space: 0.4
226981 20

Then I want to pass 226981 and 20 to integer variables. So I do:

 ifstream vfile(file_name, ios::in);      
 vfile.getline(header,FILENAME);  // Read the header-line      
 vfile >> nTot >> file_size;

If I compile the program with g++; I get for nTot and file_size the right values 226981 and 20, but If do it for Mac OS X snow leopard with the last Xcode, I get 0 and 1634000000 respectively.

Has anyone experienced this kind of error?

Thanks

+1  A: 

The call vfile.getline(header, FILENAME) is probably incorrect. The signature is:

istream::getline(char *s, streamsize n)

where s points to the output buffer and n is the size of the buffer.

I doubt that your FILENAME is an integer... it's probably a char const* that g++ implicitly casts to a streamsize? (Eeuw... use -Wall -ansi if you can.) This would have a compiler-dependent value, and if it is smaller than the length of the line, this would throw your stream into an error state (set the failbit). Subsequent reads will then fail until the error state is reset.

You should use

getline(vfile, header);

instead, where header is a std::string.

Thomas
A: 

This might be the _GLIBCXX_DEBUG issue - make sure you have the latest Xcode installed, that _GLIBCXX_DEBUG is set the same for all your code and libraries, and you might also want to check the xcode-users mailing list.

Paul R
yes, it makes more sense as I only get this error with Xcode and Snow Leopard (not Leopard before)
asdf
I found here the solution:http://stackoverflow.com/questions/1603300/xcode-3-2-1-and-c-string-fails
asdf