Jump to content
  • Advertisement
Sign in to follow this  
3dcgmodeling

static file operator

This topic is 4085 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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? }

Share this post


Link to post
Share on other sites
Advertisement
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:.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!