So, this is my problem. I just created a little file IO program. But my ifreader won't work. Here's my code:
[code]
string Class1::initialize(const char * _initFile)
{
system("cls");
Class1::consoleAttr(FOREGROUND_RED);
cout << "Init started\n";
string _ret;
ifstream _filestream(_initFile);
string _stateName;
cout << "Declared...\n";
_filestream.open(_initFile);
cout << "Opened file stream\n";
if (!_filestream.is_open()) { return "The stream is not open!\n"; }
//Read until end of file
//THIS IS NOT WORKING*********************
while (!_filestream.eof())
{
//Sleep(50); //Un-comment to make the program cooler
cout << "Trying to read line...\n";
cout << _ret;
Class1::consoleAttr(FOREGROUND_GREEN + FOREGROUND_BLUE);
cout << _stateName << endl;
getline(_filestream, _ret);
while (_ret != ":end")
{
//Sleep(50); //Un-comment to make the program cooler
cout << " " << _ret << endl;
getline(_filestream, _ret);
}
cout << "END\n";
}
//At this point it must have succeeded
cout << "Closing stream...";
_filestream.close();
cout << "Stream closed!";
return "Success!\n";
//THIS IS NOT WORKING*********************
}
[/code]
You need to tell us exactly what is not working. Simply "doesn't work" isn't a good diagnosis. If it's a segfault/access violation, use a debugger to see where it comes from.
Apart from that, your code is really strange. What are you trying to do? It looks like your second while loop will never exit unless ":end" is found before EOF.
Also, why are all your variables prefixed with an underscore? Why is your class called "Class1"?
PS there is no use for the second while loop "!_filestream.eof()" means "not end of file is reached"
Well.. as you can see i've put some cout's everywhere. That's for knowing where is breaks. It's coming all the way to "cout << "Trying to read line"" and then it wont display the text that [b]should[/b] be read from the file...
Oh.. and i'm trying to read this:
[code]
:Applications
Test1
Apptest1
Some_App
Another_One
:end
[/code]
Don't ask
Well, neither _ret nor _stateName are initialized by the time it reaches the loop that reads until ":end", meaning they are empty strings. This isn't a problem, but you won't see any text after "Trying to read line" before the file contents (barring an extra newline).
On the other hand, I can't see why it won't print out the contents of the file (if that's what you're saying doesn't happen).
I cant figure out why the text ain't writing anything. Is it the getline()?
I'll try anyway to make a simple text file reader, just to see if that works.
Doesn't ifstream take two arguments?
Try changing
[code] ifstream _filestream(_initFile);[/code]
to
[code]ifstream _filestream(_initFile, ios::in);[/code]
Sorry, you need to Log In to post a reply to this thread.