Page 1 of 1
Testing fstream files.
Posted: Mon Apr 21, 2008 8:11 am
by Falco Girgis
I was working on some advanced debuggery last night (to cancel out some of Marcel's blatant douchebaggery when it comes to loading levels).
At the moment, individual loading functions are using C-style text file manipulation:
Code: Select all
FILE *file = fopen("file.txt", "r");
That's completely sweet and all, and I can test the stream simply by:
Code: Select all
if(!file) printf("Marcel screwed something up.\n");
Now the overall debug system uses C++ fstreams (because it's newer, my older code was all C-style).
I'd like to achieve essentially the same thing:
Code: Select all
ifstream file("file.txt");
if(!file.good()) cout << "Marcel screwed something up.\n";
The problem is that the program shits itself when the file is instantiated, and doesn't even get to the good() or bad() tests (which I do believe is the correct way to check the status of the filestream.)
I'd like to know how I'd make the program continue if the file cannot be openned.
Posted: Mon Apr 21, 2008 8:40 am
by Falco Girgis
Wtf, I tried this in dev-cpp real quick:
Code: Select all
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int dummy;
ifstream file("nothere.txt", ios::in);
if(!file.good()) cout << "Screwed it.\n";
else cout << "goooood.\n";
cin >> dummy;
return 0;
}
and it does exactly what I wanted it to.
I wonder if I have some sort of setting wrong in MSVC++. Maybe it does give some sort of exception error when it quits, so maybe debug quits when exceptions are thrown and not caught?
Posted: Mon Apr 21, 2008 10:17 am
by MarauderIIC
You don't need the .good(), it's redundant (afaik, unless msvc does something else with it)
This should work:
Code: Select all
int main() {
ifstream file("doesNotExist.txt");
ifstream file2("exists.txt");
if (!file)
cout << "Error loading file" << endl; //endls flush output buffer
while (file) { //checks for .eof() and other failure conditions
doStuff();
}
if (!file2)
cout << "Error loading file" << endl;
while (file2) { //checks for eof() or other failure conditions
//(say someone deleted file while we were reading it, or is corrupt, or something)
doStuff();
}
//if you want you can do this i guess, i'm not sure if it'd work
if (!file2.eof())
cout << "An unexpected error occurred before reaching the eof" << endl;
return 0;
}
And what, does ifstream throw an exception or something? On the constructor line? Shouldn't do that unless you're out of memory or something...
Posted: Mon Apr 21, 2008 10:20 am
by Falco Girgis
Oh, wtf?
So is the logical negation operator overloaded to return false if it's screwed and true if not? Because that file isn't a pointer like it was in C.
But that's really nice, I can stick with the same notation that I'm used to.
edit: Yeah, I believe that it does, because either MSVC++ or Windows gives me some sort of exception thrown error.
Posted: Mon Apr 21, 2008 10:21 am
by MarauderIIC
I edited the post, where exactly does it barf? On constructor?
Posted: Mon Apr 21, 2008 10:22 am
by Falco Girgis
Yes, I believe so. I'll have to go home and mess with it more. I'll tell you what's happening.
Posted: Mon Apr 21, 2008 10:25 am
by MarauderIIC
Hum. (It shouldn't barf there, post the whole code, I guess. d:)
Posted: Mon Apr 21, 2008 10:27 am
by MarauderIIC
Neat. You can force it to throw exceptions on certain failures (but it doesn't default to any, of course).
http://www.cplusplus.com/reference/iost ... tions.html
Also, instead of checking for individual error states, perror() should work, or so I read (it's amazing the amount of useful stuff that classes miss and the amount of useless stuff they pack in).
Posted: Thu May 08, 2008 10:20 pm
by kilgariff
FILE *file = fopen("file.txt", "r");
Just a thought, are you guys using text files for maps?
Or when you say levels, do you mean something else, like names of items and stuff?
Posted: Fri May 09, 2008 3:21 am
by Falco Girgis
I was referring to "level" as in everything that needs to be loaded for a new area.
We read/write our maps in binary.