Page 1 of 1

File i/o help

PostPosted: Thu Dec 15, 2011 1:58 am
by SuperSonic
Hey guys sup? :D

So, I've been using GE for a while now and I've learned quite a bit thanks to everyone on the forums. However, the entire time I've been using GE, I've never once needed to use file input/outpt... until now. Now I've gained a basic understanding of io thanks to a couple of people here who where helping me with it. But I still no second to nothing. So could anyone please teach me what all of the file functions do and how to use them please? As usual, I will award points :D

Thanks in advance, Sonic

Re: File i/o help

PostPosted: Thu Dec 15, 2011 2:23 am
by skydereign
Okay, file io in C requires the use of a FILE*. All it is is a pointer to a file. You declare a FILE* just like you would any other variable, and you use fopen set a file. Namely, it opens the file up, and returns the address of the file. That way, the FILE* points to the file, and doesn't take up as much space as the entire file would. Since fopen returns a FILE* you can use it like this.
Code: Select all
FILE* file = fopen("actual_file.txt", "r");

When calling fopen, it is given two strings. First one is the name of the file, and the second one is the mode in which you open it. Different modes do different things, for instance:
"r" - will only give you read permission (meaning any functions you call to write into the file won't work) and sets you at the beginning.
"w" - will write the file, but it clears the file of any contents, so you start with an empty slate. But, since it gives you write permission, you can use functions like fprintf.
"a" - appends the file, so it opens the file up, and puts the pointer to the end of the file. That way when you use fprintf, it will add to the file, instead of overwriting your text.
"r+" - opens the file up, and allows you to write and read. Only difference from "a" is that it sets the pointer to the beginning of the file.
"w+" - opens the file for update, but discards the contents, but this allows you to read (unlike "w").
"a+" - like "w" and "w+" it sets read and write, and places you at the end of the file.
You can also append b to indicate if the file is a binary file.

Now that you have an opened file, and a handle to it, you can use the read and write functions. I'd suggest using fscanf and fprintf since most users are more familiar with how sprintf works. I can go into depth about the fprintf and fscanf functions if you want me to, but I think that's more straightforward. All it really is is writing data in a format that you can then do the reverse read operation. So, when you save data, make sure to save it in a specific format, that way you can use an fscanf to load back all the information you saved. One thing to note though is when saving information, you will usually use "w" as your fopen setting, and when you are loading information, you use "r".

And when you are done using a FILE*, so when you are done loading from it, or saving into one, you use fclose to close it.
Code: Select all
fclose(file);

Re: File i/o help

PostPosted: Thu Dec 15, 2011 2:36 am
by SuperSonic
Wow, thank you sky and +1 :D

Ok, so I think I understand how fscanf and fprintf work ok. But what if I want to actually read each character in the file and then do some if statements. Like:
Code: Select all
if(CharacterIJustRead == SomeRandomCharacter)
{
    //Do some randome code
}

Did I explain that ok? :P

Re: File i/o help

PostPosted: Thu Dec 15, 2011 3:07 am
by skydereign
Well, you can read in a bunch of characters then. You can use either fgetc which gets characters, or use fscanf to read in a character. Since I was advocating fscanf before I'll show that. Usually when you use files though you don't save the contents into a temporary variable, but instead set them to global variables so you can use them in other parts of your game (like score for displaying).
Code: Select all
void
read_function ()
{
    FILE* file = fopen("file", "r");
    char temp;

    fscanf(file, "%c", &temp);
    if(temp=='5')
    {
        // do something
    }
    fclose(file);
}

Re: File i/o help

PostPosted: Thu Dec 15, 2011 3:36 am
by SuperSonic
What does the "%c" mean in the fscanf function? :)

Re: File i/o help

PostPosted: Thu Dec 15, 2011 3:55 am
by skydereign
Ah, so you aren't familiar with the formatting used in fprintf/fscanf/sscanf/sprintf? The %c means it will read in a char, as % tells the format that there is a variable. The letter after the % tells the type it should expect, and attempts to read it in. A string is %s, an integer is %d or %i, a float is %f, and a double is %lf. There are others but those are the main ones that apply to gE.

So, in short %c means that it will read in a character. You can use other things to create a more complex read. If you use "%c%c%c", it will read in the first three characters.

Re: File i/o help

PostPosted: Thu Dec 15, 2011 2:26 pm
by SuperSonic
Ok, that makes a lot of sense. +1 again :D

Now, what if I want to read only the third character instead of the first three? :P

Re: File i/o help

PostPosted: Thu Dec 15, 2011 8:24 pm
by skydereign
Well, you can read in three characters and only use the last one, or you can use fgetc twice, and the third one is what you want. Why though would you only want the third one? If that's the case, then you should only have saved the third character. Since you control both saving and loading, you can create formats that perfectly fit what you are trying to do, so usually there isn't any redundant characters. If the two characters that you want to skip are known (as in a separator then you can use "::%c" switching :: with your separator). But, fgetc is a function that only takes your FILE*, and returns the next character. The following would store the third character into a temp var using fgetc.
Code: Select all
FILE* file = fopen("test", "r");
char third_char;
fgetc(file);
fgetc(file);
third_char = fgetc(file);
fclose(file);


And just so you know, my points have not increased for either of those. I don't mind either way, but since you mentioned it, it seems weird that it hasn't increased my score. Perhaps your account can't give points?

Re: File i/o help

PostPosted: Thu Dec 15, 2011 9:38 pm
by SuperSonic
skydereign wrote:Why though would you only want the third one?

Well, let's say I have a fraction in my file, and I only want to read the denominator. That's why I would want to do that :D
skydereign wrote:If the two characters that you want to skip are known (as in a separator then you can use "::%c" switching :: with your separator)

Oh that's so cool. I had no idea you could do that :D
skydereign wrote:And just so you know, my points have not increased for either of those. I don't mind either way, but since you mentioned it, it seems weird that it hasn't increased my score. Perhaps your account can't give points?

Hmm, wierd. I know I clicked it. I'll do it again. And thanks :P
*EDIT: Ok, I clicked it again and your score says 335 now :)

Re: File i/o help

PostPosted: Thu Dec 15, 2011 9:45 pm
by skydereign
Weird, I've never had to use fractions. After all, if you wanted to save a fraction, you could just save an integer, separated by a delimiter (in that case /). So you can read in two integers, "%d/%d". If you use characters, then either you are using character division, or are just limiting yourself to one digit values for the denominator. But even then, I've never felt that fractions would help. What do you happen to be doing?

Re: File i/o help

PostPosted: Thu Dec 15, 2011 11:51 pm
by SuperSonic
I'm currently working with graphing functions. You know the basic function right? Y = mX + b. Since the slope is usually a fraction, I would need to able to access the nominator and demoninator to find the slope and such. You know what I mean? :D