Thread: trouble reading from a bin. file into a dynamic array of structures

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

    trouble reading from a bin. file into a dynamic array of structures

    So I am having trouble reading from a bin file (inside a directory), and storying the contents into an array of structs dynamically. I am getting an error saying "each struct field must be read individually", this is part of the requirements.

    Is there an obvious reason I would be having trouble reading the fields. Should I be trying to read them straight into struct stage3 user or create a new array in memory?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct stage3
    {
            double cream;
            short int apple;
            int brush;
            unsigned long finger;
            char boy [10];
            short int sight;
            long int ocean;
            unsigned short shovel;
            char water;
    };
    
    
    int main(int argc, char** argv)
    {
            int i;
            size_t size;
            struct stage3 *user;
            FILE *fptr;
    
    
            if (argc < 2) { 
                    fprintf(stderr, "Usage");
                    exit(1);
    }
            if ((fptr = fopen(argv[1], "rb+")) == NULL) {
                    fprintf(stderr, "could not open file: %s\n", argv[1]);
                    exit(1);
    }
                    fseek(fptr, 0, SEEK_END);
                    size = (ftell(fptr));
                    fseek(fptr, 0, SEEK_SET); 
    
    
                    user = malloc(size); 
    
            if (user == NULL) {
                    fprintf(stderr, "memeory exhausted\n");
                    exit(1);
    }
                    i = 0;
            while(1) {
                    fread(&user.cream, sizeof(int), 1, fptr);               
                    fread(&user.apple, sizeof(double), 1, fptr);
                    fread(&user.brush, sizeof(int), 1, fptr);
                    fread(&user.finger, sizeof(short int), 1, fptr);
                    fread(&user.boy, sizeof(int), 1, fptr);
                    fread(&user.sight, sizeof(unsigned long), 1, fptr);
                    fread(&user.ocean, sizeof(char), 10, fptr);
                    fread(&user.shovel, sizeof(short int), 1, fptr);
                    fread(&user.water, sizeof(long int), 1, fptr);
                    
                    if (feof(fptr)) {
                    break;
                    i++;
    }
                    fclose(fptr);
                    free(user);
            return 0;
    }
    Last edited by copernicus1996; 08-16-2017 at 08:16 PM.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You can't just randomly assign types to the fields in the fread statements. They actually have to match the types in the struct.

    And since you're reading them into an array, you need to index user, like user[i].cream.

    You should print the contents afterwards to see if you've read them correctly.

    Also, your indentation is wacky.

    EDIT:
    You're also not testing for end-of-file correctly. You should do it with the first fread statement:
    Code:
    while (fread(...) == 1) {  // assuming the 3rd parameter is 1
        fread(...);
        ...
        i++;
    }
    Also, I probably wouldn't determine the amount of memory to allocate the way you have, but it might be okay as long as the file was written with the same compiler/settings/OS.
    Last edited by algorism; 08-16-2017 at 09:06 PM.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    instead of matching size to the type in the struct you can just use the size of variable you are reading (assuming as was said - read and write are performed by same compiler settings build exe)

    Code:
    if (fread(&user[i].cream, sizeof(user[i].cream), 1, fptr) != 1){
       //error
       break; 
    }
    you also have to check against the out of bounds access
    so since you allocated size elements - I woudl write a loop as
    Code:
    (for i=0; i< size; i++)
    so you never try to write post your allocated array
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-02-2014, 04:39 PM
  2. Replies: 15
    Last Post: 06-17-2012, 04:27 PM
  3. trouble reading a text file into an array
    By Ciaran in forum C Programming
    Replies: 15
    Last Post: 04-04-2011, 05:14 PM
  4. Reading the file, and filling the array of structures
    By BlackOps in forum C Programming
    Replies: 13
    Last Post: 07-12-2009, 02:03 PM
  5. Reading from file into array of structures.
    By Sinensis in forum C Programming
    Replies: 1
    Last Post: 12-02-2008, 01:22 AM

Tags for this Thread