Thread: Binary file output

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    3

    Binary file output

    so I have this problem, I want to write array of numbers to binary file and then read them from it and write them into another array, but I get some wrong numbers, how do i fix it ?
    Code:
    #include <stdio.h>#include <conio.h>
    
    
    int main() {
    
    
        int num_arr[9]; int m = 0;
        FILE *fp;
        fp=fopen("test.bin", "rb");
        int x[10] = { -1,-3,-2,-5,-6,-7,-8,-9,-4 };
        for (int i = 0; i <= 9; i++) {
            fwrite(&x[i], sizeof(int), 9, fp);
        }
        
        for (int i = 0; i <= 9; i++) {
            fread(&m, sizeof(int), 9, fp);
            num_arr[i] = m;
            printf("%d\n ", num_arr[i]);
        }
    
    
        printf("\n");
        
        int y = 0, k = 0, z = 0;
        for (int j = 0; j <= 9; j++) {
            if (y < num_arr[j])
                y = num_arr[j];
    
    
            if (k < num_arr[j] && num_arr[j] < y)
                k = num_arr[j];
    
    
            if (z < num_arr[j] && num_arr[j] < y && num_arr[j] < k)
                z = num_arr[j];
        }
    
    
        printf("Three max nums in array: \n%d\n%d\n%d", y, k, z);
    
    
        getch();
    
    
    }

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    You need a rewind() or an fseek() between the fwrite(), and fread().

    Please do not use conio.h and getch(). They are specific to some Windows based compilers, and are NOT Standard C!!!

  3. #3
    Registered User
    Join Date
    Nov 2018
    Posts
    3
    can you explain a bit please ?

  4. #4
    Registered User
    Join Date
    Nov 2018
    Posts
    3
    Code:
    int num_arr[9]; int m = 0;    FILE *fp;
        fp = fopen("test.bin", "rb");
        int x[10] = { -1,-3,-2,-5,-6,-7,-8,-9,-4 };
        for (int i = 0; i <= 9; i++) {
            fwrite(&x[i], sizeof(int), 9, fp);
        }
        rewind(fp);
        fseek(fp, sizeof(int), SEEK_SET);
        for (int i = 0; i <= 9; i++) {
            fread(&m, sizeof(int), 9, fp);
            num_arr[i] = m;
            printf("%d\n ", num_arr[i]);
        }
    is it ok?
    but it doesn`t work

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    I said you need a rewind() OR an fseek(), but NOT both!!!

    Remove line 8, the fseek(). A rewind in your case is fine.

    Two reasons for one of these functions. Whenever you write to a file opened as you have, the read from it, you need to flush the buffers. You do this with a rewind() or an fseek().

    In your case you have written 9 ints, probably 36 bytes into the file. In order to read the data you have written to the file, you need to reset the position in the file back to 0, or the first byte in the file. You are doing that with a rewind().

    Also, I didn't catch it before, since I did not compile the program, if you are using a for() loop 9 times to write and read the data, you need to specify 1 int not 9:
    Code:
    fwrite(&x[i], sizeof(int), 9, fp);
    // Should read:
    fwrite(&x[i], sizeof(int), 1, fp);
    // The same for the fread()
    You also should check the return value from fopen() to insure that the file was opened, fwrite() to insure data has been written, and fread() to insure you have read the data you expect to read.

    Start with this and if you still have problems, please repost your whole program, and I will compile and test it.

    Please make sure your warning level in your compiler is set to the highest level.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    What type of variable is m? What exactly are you trying to read into this variable?

  7. #7
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    Quote Originally Posted by wztch View Post
    Code:
    int num_arr[9]; int m = 0;    FILE *fp;
        fp = fopen("test.bin", "rb");
        int x[10] = { -1,-3,-2,-5,-6,-7,-8,-9,-4 };
        for (int i = 0; i <= 9; i++) {
            fwrite(&x[i], sizeof(int), 9, fp);
        }
    You write in "x[]" not in num_arr[]. So, in num_arr[] is nothing.

    Code:
    Code:
        rewind(fp);
        fseek(fp, sizeof(int), SEEK_SET);
    Not both.

    For example:
    Code:
    . . .
    int num_arr[10]; //For nine numbers
    int result;
    . . .
    
    
    //For read
    //File pointer to the beginning
    result = fseek(fp, 0L, SEEK_SET);
    if (result)
    {
        //If result is not 0; -1 would be an error
        perror("fseek not possible");
    }
    else
    {
        fgets(num_arr, 9, fp);
        printf("%s", num_arr);
    }

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Look closely at your read() and write() calls. What exactly is the purpose of the third parameter?

    You may also want to take a look at your file with a hex editor/dump program to see exactly what you actually wrote to the file.
    Last edited by jimblumberg; 11-25-2018 at 05:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-07-2013, 04:33 PM
  2. Replies: 6
    Last Post: 12-06-2013, 11:39 PM
  3. GCC binary output file options
    By Pyroteh in forum Linux Programming
    Replies: 1
    Last Post: 01-08-2007, 09:15 PM
  4. Binary Output
    By Ajsan in forum C++ Programming
    Replies: 9
    Last Post: 04-23-2004, 04:43 PM
  5. Specifying binary output?
    By Hardboy in forum C++ Programming
    Replies: 3
    Last Post: 04-10-2003, 03:41 PM

Tags for this Thread