Thread: call a function read from binary file and fill array with data

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    2

    call a function read from binary file and fill array with data

    I need help.How do I

    - define a array of struct of struct 5
    -call a function read from binary file and fill array with data
    - display the array to user
    - user selects one set of array values

    this is what I have so far

    insert
    Code:
    void getInput(STOREDDATA *iptr)
    {
         int values[5]; //values is an array of 5 integers
        int i;
        array=fillTheArrayfromBinaryFile(values);
    }
    i am having troubles with displaying the array to the user
    and the user selects one set of array values

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well there's not a lot to go on.

    Here's an example of how to declare arrays, structs, and read/write them to files in binary mode.
    Code:
    #include <stdio.h>
    
    #define LEN 5
    
    struct foo {
      int a;
    };
    
    int main( ) {
      struct foo array[LEN];
    
      // fill it
      for ( int i = 0 ; i < LEN ; i++ ) {
        array[i].a = i;
      }
    
      // write it
      FILE *fp = fopen("testfile1.bin","wb");
      for ( int i = 0 ; i < LEN ; i++ ) {
        fwrite(&array[i].a,sizeof(array[i].a),1,fp);
      }
    
      // Same again, but write a whole struct instance at once
      fp = fopen("testfile2.bin","wb");
      for ( int i = 0 ; i < LEN ; i++ ) {
        fwrite(&array[i],sizeof(array[i]),1,fp);
      }
      fclose(fp);
      
      struct foo input[LEN];
      
      // read it
      fp = fopen("testfile2.bin","rb");
      for ( int i = 0 ; i < LEN ; i++ ) {
        fread(&input[i],sizeof(input[i]),1,fp);
      }
      fclose(fp);
      
      // print it
      for ( int i = 0 ; i < LEN ; i++ ) {
        printf("Value at index %d is %d\n", i, input[i].a);
      }
      return 0;
    }
    Results
    Code:
    $ gcc main.c
    $ ./a.out 
    Value at index 0 is 0
    Value at index 1 is 1
    Value at index 2 is 2
    Value at index 3 is 3
    Value at index 4 is 4
    $ hd testfile1.bin 
    00000000  00 00 00 00 01 00 00 00  02 00 00 00 03 00 00 00  |................|
    00000010  04 00 00 00                                       |....|
    00000014
    $ hd testfile2.bin 
    00000000  00 00 00 00 01 00 00 00  02 00 00 00 03 00 00 00  |................|
    00000010  04 00 00 00                                       |....|
    00000014
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to read a binary file in 2d array?
    By sherry91 in forum C Programming
    Replies: 5
    Last Post: 03-16-2016, 12:53 AM
  2. Read data from binary file with condition
    By tawawogita in forum C Programming
    Replies: 11
    Last Post: 06-01-2015, 03:25 PM
  3. Replies: 2
    Last Post: 05-06-2011, 07:43 AM
  4. Replies: 21
    Last Post: 11-03-2007, 02:56 PM
  5. Function to read data from file to array
    By anatazi in forum C Programming
    Replies: 3
    Last Post: 07-01-2002, 11:08 PM

Tags for this Thread