1) You've got your member variables defined inside the function instead of the class body.2) Your class is missing a terminating semicolon (';')3) Your main is returning a return type ('int').4) Since main() can only run once (legally), there's no point in maing myfile static -- it will only be constructed once anyways.5) Your constructor is before the public:, and therefore private:.

Share this post

Link to post

Share on other sites

Original post by 3dcgmodelinghello , It seem I found the problem. in my home pc that use vs2005,the above source code work fine. if in my office pc that use vc6, it return ok 2 times and then fail the left loop.

Are the files you're trying to read the same on both machines? If on your office PC the file only contains two words, that would cause this (the third read would try to read past the end of the file, causing eof(), causing !file.good(), causing isok() to return false).

Quote:

Original post by JohnBoltonThere is a problem. You are opening the file for binary input (ios::binary), but using the >> operator, which is intended for formatted text only -- not binary.

Shouldn't be the problem. The only affect is it will not automatically convert newlines, which is sometimes desireable. "ios::binary" is really a bit of a misnominer in this regard, especially when output operations to fstreams with ios::binary set will still be in text when using operator<<.

This however is an abuse of static:

void function(){for(int i=0;i<10;i++) {static readfile f("text.txt");

if(f.ifok()) MessageBox(0,L"ok",0,0);else MessageBox(0,L"fail",0,0);

f.read(); }

}

What if you wanted to reread the file? You couldn't just call function() again, because static variables are only made one for the entire lifetime of the program. While this is sometimes useful, it's abusive to use here. This is what you want instead: