Testing fstream files.

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Testing fstream files.

Post 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.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post 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?
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Post 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...
Last edited by MarauderIIC on Mon Apr 21, 2008 10:21 am, edited 1 time in total.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post 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.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Post by MarauderIIC »

I edited the post, where exactly does it barf? On constructor?
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post 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.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Post by MarauderIIC »

Hum. (It shouldn't barf there, post the whole code, I guess. d:)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Post 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).
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
kilgariff
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 17
Joined: Wed May 07, 2008 8:45 pm
Current Project: ArkEngine, Regalia
Favorite Gaming Platforms: PC (Linux, Windows)
Programming Language of Choice: C++ and Assembly
Location: Scotland
Contact:

Post 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?
Carmack: "The most important thing is to try and learn things deeply. Don't try for a superficial knowledge of a lot of things. I've gotten where I am from knowing everything deeply, down to the lowest level."
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post 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.
Post Reply