Binary FIle I/O

This is a discussion on Binary FIle I/O within the C Programming forums, part of the General Programming Boards category; Code: void create_customer() { FILE *customer_fp; customer_indexed indexed_array[FILE_SIZE]; //255 customer_record new_record; int i; customer_fp = fopen("test.bin", "wb"); printf("Input customer first ...

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    Binary FIle I/O

    Code:
    void create_customer()
    {
            FILE *customer_fp;
            customer_indexed indexed_array[FILE_SIZE]; //255
            customer_record new_record;
            int i;
            
            customer_fp = fopen("test.bin", "wb");
                    
            printf("Input customer first name: ");
            scanf("%s", new_record.first_name);
            printf("Input employee last name: ");
            scanf("%s", &new_record.last_name);
            fwrite(&new_record, sizeof(new_record), 1, customer_fp);
            fprintf(customer_fp, "\n");
            fclose(customer_fp);
    }
    I am trying to write this to a binary file. It wont be implemented using scanf later, its just to see if its working for now.

    I have another file to read the binary file, but it will only allow me to add one entry. I am calling the function twice in main.

    Any suggestions?

    Thanks
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,681
    You don't need the "\n" since it's a binary file - no need to seperate the records with anything.
    Reading and writting structures in this way means that your are using fixed sized records in the file. This means you must have fixed char arrays in custormer_record for first_name and last_name. If thats true, then it looks good (except for the "\n").

    Just as a good programming habbit, I like to use sizeof() on the structure type rather than the structure variable (ie. sizeof(custormer_record)) to avoid accidentally using sizeof() on a pointer.

    gg

  3. #3
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Yes I do have fixed sizes for them.

    I took out the \n, I thought that was why it wasnt putting the data into the file.

    Still, nothing appears in the file.

    Code:
            customer_record new_record;
            int i,j;
            FILE *fp;
                    
            fp = fopen("test.bin" ,"rb");
            while(1 == fread(&new_record, sizeof(customer_record),1,fp))
                    printf("%s %s\n" ,new_record.first_name, new_record.last_name);
                    
            fclose(fp);
    That is the printing of the binary file.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,681
    When you open the file, the file pointer for writting is at the beginning of the file. So the second time you call your create_customer() function, you are overwritting the last record you wrote. You will want to move the file pointer to the end of the file, or open the file for appending with "a+" in the mode.

    gg

  5. #5
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Thanks for the help.

    I open the file with an editor instead of using the read function.

    It seems like only the first scanf is inputed into the file.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>open the file for appending with "a+"
    Don't forget binary mode is also required.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  2. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-17-2005, 11:09 PM
  3. Problem with Binary File I/O
    By DirX in forum C++ Programming
    Replies: 4
    Last Post: 03-01-2004, 08:34 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. I/O to binary file
    By pors7 in forum C++ Programming
    Replies: 2
    Last Post: 12-07-2001, 01:27 PM

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