0

Is there a way to determine how many elements an array of struct should have or allocate dynamic memory for a struct array in C? The code opens a binary file and reads it into a struct array. I can place an arbitrary value like 3000, however when I go to print it gives me the garbage values after the file ends. I'm looking for a way to set my array to the size of the file that is being read or limit it to the useful data. I imagine a function like malloc() just haven't been able to find examples with struct arrays that aren't static.

#include "function.h"


int main() {
    FILE *fp; // file pointer

    STRUCTNAME structVar[]; //typdef array of structs

    fp = fopen("file.bin", "rb"); //binary file to read into array

    if(fp == NULL) { // error check file opening
        printf("error\n");

    } else {

    //while !feof to read file into array
    while(!feof(fp)) {
        fread(structVar, sizeof(structVar), 1, fp);

        printStructData(structVar);
    }

    fclose(fp); // closes file
    }
  return 0;
}
Kieta
  • 1
  • 4
  • For starters, `while(!feof(file))` is always wrong. https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Christian Gibbons May 24 '18 at 15:26
  • Please format correctly your code. As for your question, you might as well just read the size of the file and divide it by the size of your struct and use that as the number of elements that should go in your dynamic array. Then you just malloc the size of your struct by the number of elements. – Valerio Santinelli May 24 '18 at 15:29
  • I saw that article when troubleshooting an earlier problem with the code, I've used other while loops to read the file. Originally I wouldn't output any information and i realized it was an issue with the development environment. Thank you for the link. – Kieta May 24 '18 at 15:32
  • Possible duplicate of [How can I get a file's size in C?](https://stackoverflow.com/questions/238603/how-can-i-get-a-files-size-in-c) – StereoBucket May 24 '18 at 15:33
  • It is similar and determining the file size does answer the first part of my question. But, I also wasn't sure how to malloc the struct array. I've seen examples of struct, static struct array, I don't believe I've seen any for dynamic struct array. If there is a reference I'd be happy to take the question down. – Kieta May 24 '18 at 16:05

1 Answers1

0
STRUCTNAME *structVar;

FILE* fp = fopen(...);
if(fp) 
{
  long fileSize;
  fseek(fp, 0 , SEEK_END);
  fileSize = ftell(fp);
  fseek(fp, 0 , SEEK_SET);
  structVar = malloc(sizeof(STRUCTNAME) * (fileSize / sizeof(STRUCTNAME) + 1));  //just in case if the file length is not exactly sizeof(STRUCTNAME) * n
  ...
  fclose(fp);
}
0___________
  • 60,014
  • 4
  • 34
  • 74
  • Ok, so I can use fseek to determine my file size and from that work the size of the array. Thank you. – Kieta May 24 '18 at 16:06
  • Please mark the answer as correct if it satisfies you, to thank the person who helped you. – r3mus n0x May 24 '18 at 16:16
  • I don't see an option to mark it as correct, I see a answer your question button below and the duplicate question button or edit above. – Kieta May 24 '18 at 20:18