static file operator

Started by
5 comments, last by MaulingMonkey 16 years, 12 months ago
class file { file(char * filename):ifile(filename,ios::in) { public: //bla bla interface private: ifstream ifile; } } main() { static file myfile("filename"); // it fail as i know, static variable should defined once,then call construct once. so it should succeed.why? }
Advertisement
Quote:why?


Why what?
This post fails to:

1) Provide anything resembling legal C++ code. Please post actual source demonstrating the error in question.
2) Provide actual error messages or symptoms encountered. "it fail" could mean a million things.
3) Use [source] tags.

Please try again. If you're actually trying to compile that snippet:

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:.
thanks for your lines.
#include <fstream>#include <string>#include <windows.h>using namespace std;class readfile{public:	readfile(char * filename):ifile(filename,ios::in | ios::binary)	{	}	bool ifok()	{		if(!ifile)			return false;		return true;	}	void read()	{		char c[20];		//ifile.read(c,10);		string cc;		ifile >> cc;	}private:	ifstream ifile;};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();	}	}int main(){	function();	return 0;}  


what does "it fail" (in vc6) mean is : ifok() return false, but it success in vc7.
hello , 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.
There 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.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote:Original post by 3dcgmodeling
hello , 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 JohnBolton
There 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:

void function(){	readfile f("text.txt");	for(int i=0;i<10;i++)	{		if(f.ifok())			MessageBox(0,L"ok",0,0);		else			MessageBox(0,L"fail",0,0);		f.read();	}	}


You use static to replace globals, not to replace a variable in an outer scope.

This topic is closed to new replies.

Advertisement