开发者

g++ and file read, weird behaviour with Xcode/Snow Leopard

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 t开发者_运维技巧he 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜