C file handling

This is a discussion on C file handling within the C Programming forums, part of the General Programming Boards category; I have been try OVER and OVER to make a C program that opens a file and parses it. Can ...

  1. #1
    Satan valar_king's Avatar
    Join Date
    Aug 2001
    Posts
    288

    C file handling

    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.

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    I don't know what you mean with parsing a file. But here are some hints which I hope help you further.

    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);
    Note: a byte is a variable of type unsigned char.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > 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.

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Yep. Correct, forgot that.

    Code:
    fputc (byte, destination_file);
    This destination_file should be opened as binary updating, which was "r+" I guess.

    Code:
    destination_file = fopen ("destination_file.dat", "rb+");
    >Additionally, there is no 'byte' data type in C.

    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 01:54 PM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > 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);

  6. #6
    Satan valar_king's Avatar
    Join Date
    Aug 2001
    Posts
    288

    Post Here's what I meant

    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:

    Code:
    struct pixel {
     int x, y, z;
     int r, g, b;
    };
    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 vertex {
     float x, y, z;
     float u, v;
    };
    
    struct quadface {
     vertex points[4];
    };
    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.

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating File Handling Functions
    By td4nos in forum C Programming
    Replies: 6
    Last Post: 06-26-2009, 11:43 AM
  2. basic file handling problem
    By georgen1 in forum C Programming
    Replies: 4
    Last Post: 03-05-2009, 05:21 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21