I have been try OVER and OVER to make a C program that opens a file and parses it. Can anyone explain the C system or just point me to a tutorial that does? And what can I use to make binary files?
Thanks,
Valar_King
I have been try OVER and OVER to make a C program that opens a file and parses it. Can anyone explain the C system or just point me to a tutorial that does? And what can I use to make binary files?
Thanks,
Valar_King
-Save the whales. Collect the whole set.
I don't know what you mean with parsing a file. But here are some hints which I hope help you further.
Note: a byte is a variable of type unsigned char.Code:FILE *myfile; /* Open the file myfile.dat as binary for reading */ myfile = fopen ("myfile.dat", "rb"); while (!feof (myfile)) { byte = fgetc (myfile); /* read a byte */ fputc (byte); /* write a byte */ } /* Close the file */ fclose (myfile);
> fputc (byte);
Slight problem: You didn't specify the file to put to. Potential secondary problem: You've only opened one file, and since you aren't using update mode, you can't put to it.
Additionally, there is no 'byte' data type in C.
Quzah.
Hope is the first step on the road to disappointment.
Yep. Correct, forgot that.
This destination_file should be opened as binary updating, which was "r+" I guess.Code:fputc (byte, destination_file);
>Additionally, there is no 'byte' data type in C.Code:destination_file = fopen ("destination_file.dat", "rb+");
What I meant was: if you want to use bytes, the variables which are representing the bytes should be of type unsigned char.
Code:typedef unsigned char byte;
Last edited by Shiro; 01-22-2002 at 02:54 PM.
> typedef unsigned char byte;
Which is no good for storing EOF, so you do not detect the end of file properly
The archetype copy a file a byte at a time is
Code:int ch; // must be int FILE *myfile; /* Open the file myfile.dat as binary for reading */ myfile = fopen ("myfile.dat", "rb"); while ( (ch = fgetc (myfile) ) != EOF ) { fputc (ch, stdout ); } /* Close the file */ fclose (myfile);
By parsing I meant going through the file and looking for data. The thing I am learning this for is 3d file formats in OpenGL. NeHe has a good tutorial that will hand me the code, but I'd much rather learn how to work with files. Say I make a struct like this:
That's an example struct I could use. I'd like the program to open a file and read values for an array of pixel or other types of structs. Another example closer to my final goal.Code:struct pixel { int x, y, z; int r, g, b; };
I'd like some code to read a file and grab the data for that points object according to a pattern or format. The type of file I am working towards is .md2 Quake2 models, but that's a long way off.Code:struct vertex { float x, y, z; float u, v; }; struct quadface { vertex points[4]; };
I hope that explains what I am trying to do. Any help would be appreciated. Thanks for the quick replies.
Thanks,
Valar_King
BTW What program should I use to make binary files?
-Save the whales. Collect the whole set.