check extensions

Started by
17 comments, last by SiCrane 14 years, 12 months ago
I don't really see why shutting down the game would be sub-optimal behavior. I guess if my program was fed a bad .ani file then I could just skip the animation. But monsters and stuff rely on files as well. If the program finds that it is trying to read a corrupt monster file then I'd rather just exit and say "Bad monster file." That seems pretty optimal to me.
Advertisement
For your game, today, it most likely does not matter. But if, say, Blizzard's Battlenet servers shut down every time someone with an out-of-date (or pirated) copy of Starcraft attempts to connect using an invalid protocol, or has a slight network glitch (for which networking protocols like TCP already compensate much) there would much howling on the Internet.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Well yeah. I just mean for my game, say a new map is trying to load and it wants to load some monster called a gremlin. If it tries loading the gremlin file and the file doesn't make any sense I think I have two options: 1-Continue the game but just don't load a monster or 2-Exit. I think I'd rather exit.
Quote:Original post by icecubeflower
Why does ifstream successfully open a directory?

Sometimes I want infile.open(whatever) to fail if it's fed a non-existent file name. But if I pass it something like "home/icecube/" it opens successfully even though it didn't open a file. So then it tries infile>>inum or something and my program crashes.


So write the program in such a way that it doesn't crash if an input request fails. I've already linked you to a resource that explains what happens when input requests fail. Here's another.
Yeah I get it.
You know, a program actually crashes instantly if you open a directory as a file and try reading into an int. I just rewrote a bunch of stuff, checking to see if file I/O failed and it was useless. If you open a legitimate file and keep reading in after you hit eof it just sets the fail bits to true. If you try reading in from a ifstream that opened a directory=instant crash. I guess I have to actually make sure I didn't open a directory.
Suppose I programmed a lot of structures. So one structure contains another. Suppose the outer structure opens a file and checks to make sure that it didn't open a directory. Then it passes the stream to another structure. So I know that the sub structure does not need to check anything about the file.

Is that bad? Should I make any function that is passed a stream check the stream even though I know the outer structure checked it already?

I mean realisitically I know it would only matter if someday someone used one of my inner structures in a different project and then nothing would be checking the stream. And I seriously doubt that's going to happen. But I guess I want to do the professional thing either way.
neat stuff:
("crud/edumacation"=file "crud/darkimage"=directory")
#include <fstream>#include <iostream>#include <sys/stat.h>#include <sys/types.h>#include <unistd.h>int main(){  std::ifstream infile;  int ival;  int ix;  struct stat statbuf;  stat("crud/edumacation", &statbuf);  if(S_ISREG(statbuf.st_mode))    std::cout<<"crud/edumacation is a regular file\n";  else    std::cout<<"crud/edumacation is not a regular file\n";  if(S_ISDIR(statbuf.st_mode))    std::cout<<"It's a directory\n";  stat("crud/darkimage",&statbuf);  if(S_ISREG(statbuf.st_mode))    std::cout<<"crud/darkimage is a regular file\n";  else    std::cout<<"crud/darkimage is not a regular file\n";  if(S_ISDIR(statbuf.st_mode))    std::cout<<"It's a directory.\n";  return 0;}


output:
icecube@inferno:~$ filecrashcrud/edumacation is a regular filecrud/darkimage is not a regular fileIt's a directory.icecube@inferno:~$


One thing I don't understand at all though is why do I have to say:
struct stat statbuf;

Why can't I just say:
stat statbuf;

That doesn't make any sense to me at all.
stat is both a struct name and a function name. You need to say struct stat to specify that you want the struct name rather than the function name.

This topic is closed to new replies.

Advertisement