reading from file using a function problem
Code:
void Lines::load(string filename)
{
string lines;
ifstream inFile;
inFile.open(filename.stx);
if (!inFile)
{
cout<<"error";
}
while (!inFile.eof())
{
while(inFile.eof() != '/0')
{
getline(inFile,lines);
}
}
i have a function : load(string filename)
which performs the read file function the "string filename" is the name of the file i input thought "cin", but the function can't seem to be working properly in inFile.open(filename.stx).
any help would be appreciated :D
Re: reading from file using a function problem
Re: reading from file using a function problem
thanks for reply monarch, the .stx is the extension of the file that i'm supposed to open...
i've included the .c_str() but still couldn't get it working :'(
Re: reading from file using a function problem
Don't confuse the string object, and the character array.
filename is a string. filename.c_str() is a character array.
filename should already contain "youfilename.ext". If it doesnt contain that, than you can do this:
Code:
filename+=".stx";
ifstream inFile;
inFile.open(filename.c_str());
Re: reading from file using a function problem
thanks alot monarch, you've been a great help =)
i've managed to read the whole file and print it out using now, but i still have a question.
Assuming my files have the words => "Hi how are you doing."
i have the 5 string variables:
string a,b,c,d,e;
i wanna input from the file Hi to a, how to b are to c and so on, how do i do so?
Re: reading from file using a function problem
your file is a stream, so it works the same ways as cin and cout, so...
Code:
myFile >> a;
myFile >> b;
myFile >> c;
myFile >> d;
myFile >> e;
if ( !myFile ) {std::cout << "an error occured" << std::endl;}
Re: reading from file using a function problem
in >> a >> b >> c >> d >> e
Also, your eof-checking can be simplified:
Code:
while (getline(inFile,lines))
{
...
}