Sign in to follow this  
Samsonite

variable std::ifstream file' has initializer but incomplete type

Recommended Posts


bool CGameGame::GetStory(string filename)
{
     //open up the file for reading
     ifstream file (filename, ios::in);
     //if the file is not open
     if(!file.is_open())
     {
                        cout << "Could not open file " << filename << "!"<<endl;
                        //return failure
                        return false;
     }
     //"Mark" the point in file
     l = file.tellg();
     //Search for the end
     file.seekg(0,ios::end);
     //"Mark" the end point
     m = file.tellg();
     //Search for the beginning
     file.seekg(0,ios::beg);
     //Allocate a char buffer with the size of the file
     filebuffer = new char[m];
     //While the end of file is not reached
     while(!file.eof())
     {
                       //Read each line into the buffer called "filebuffer"
                       file.getline(filebuffer, 100);
     } 
     //Then return true;
     return true;
}



This little piece of code returns
54 C:\Dev-Cpp\Data-drive gamesz\AGClass.h variable `std::ifstream file' has initializer but incomplete type 



So what am I doing wrong? Help is much appreciated!

Share this post


Link to post
Share on other sites
Have you included the fstream header file?



#include<fstream>

int main()
{
std::ifstream myfile("filename.txt");
}





You also should not need to specify the std::ios::in flag as a second parameter for the ifstream constructor, as it is implicit for an ifstream, provided through a default constructor argument.


explicit basic_ifstream(const char* s, ios_base::openmode mode = ios_base::in);




There is also a cool trick for reading a file into a string, which avoids the loops and explicit memory allocation, it goes something like:


#include<fstream>
#include<sstream>
#include<string>

std::string GetStory()
{
std::ifstream file("filename.txt");
std::stringstream ss;
ss << file.rdbuf();
return ss.str();
}

Share this post


Link to post
Share on other sites
Use ifstream file (filename.c_str(), ios::in); instead. And you don't need to specify ios::in, by the way.
The reason you need to specify .c_str() is that you are passing in a std::string object, and the constructor expects a char *. std::string does not have implicit char * conversion, so you must use the c_str() method to "convert" to a char *.


jfl.

Share this post


Link to post
Share on other sites
@jfl:

Now I can see the problem. But even though I changed it, it didn't work.

So now my code looks like this:


bool CGameGame::GetStory(string filename)
{
//open up the file for reading
ifstream file (filename.c_str(), ios::in);
//if the file is not open
if(!file.is_open())
{
cout << "Could not open file " << filename << "!"<<endl;
//return failure
return false;
}
//"Mark" the point in file
l = file.tellg();
//Search for the end
file.seekg(0,ios::end);
//"Mark" the end point
m = file.tellg();
//Search for the beginning
file.seekg(0,ios::beg);
//Allocate a char buffer with the size of the file
filebuffer = new char[m];
//While the end of file is not reached
while(!file.eof())
{
//Read each line into the buffer called "filebuffer"
file.getline(filebuffer, 100);
}
//Then return true;
return true;
}

Share this post


Link to post
Share on other sites
The iostream header file only declares the following objects



namespace std {
extern istream cin;
extern ostream cout;
extern ostream cerr;
extern ostream clog;
extern wistream wcin;
extern wostream wcout;
extern wostream wcerr;
extern wostream wclog;
}




Some implementations also include the <ostream> header and the <istream> header, but they are not required to by the C++ standard.

The ifstream object is declared in the <fstream> header as


typedef basic_ifstream<char> ifstream;



So you must include that header.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this