0
votes

I am working on a project where I need to read data from a binary file. I am trying to store the data into a char buffer. Suppose the binary file consisted of a character, an int and a double what size would the char buffer need to be ? And how would I convert back into int's and doubles ?

I am reading the data into a char buffer because it would improve the speed of my program.

Thanks!

2
You should do basically the same as the program creating the file, but instead of fwrite you use fread. The speed issue is a red herring, it won't be noticeable if you do one or three calls to fread, specially since fread is reading from a buffer so in a way it already is in a "char buffer". - Some programmer dude
Its a pre created binary file and it has like 10 million lines. So it kind of will make a difference. I am just wondering how the data would get stored into a char buffer. and what size the buffer should be and how to convert them back to ints and doubles. I am positive it will make a reasonable difference in time - RagHaven
Like I said, fread is buffered, so it already extracts data from a "char buffer". My suggestion is that you try both ways and time it. If you really want to know the size of the data-types, use sizeof. - Some programmer dude
Does it make sense to count lines for a binary file? - alk

2 Answers

0
votes

The following example program fread()s the first DATASIZE sets of a char, an int and a float from a file specified on the command line:

typedef struct Data_s {
  char c;
  int i;
  float f;
} Data_t;

#define DATASIZE 3

int main(int argc, char ** argv) {
  if (1 >= argc) {
    fprintf(stderr, "usage: %s <file name>\n", argv[0]);
    return EXIT_SUCCESS;
  }

  {
    FILE * f = fopen(argv[1], "r");
    if (!f) {
      perror("fopen() failed.");
      return EXIT_FAILURE;
    }

    {
      Data_t data[DATASIZE];
      size_t sizeData = sizeof(*data);
      size_t sizeToRead = sizeof(data)/sizeData;
      memset(data, 0, sizeToRead * sizeData);
      size_t sizeRead = fread(&data, sizeData, sizeToRead, f);
      if (0 != fclose(f))
        perror("fclose() failed,");
      if (sizeToRead != sizeRead) {
        perror("fread() failed.");
        return EXIT_FAILURE;
      }

      for (size_t i = 0; i < sizeToRead; ++ i)
        printf("read c=0x%02hhx, i=%d, f=%f from '%s'\n", data[i].c, data[i].i, data[i].f, argv[1]);
    }
  }

  return EXIT_SUCCESS;
}
0
votes

You can use the fscanf function to read the data from the file straight into eagerly awaiting variables:

char c;
int i;
double d;
FILE *fp = fopen("C:\\example.txt", "rb");
if (fp)
{
    fscanf(fp, "%c%d%lf", &c, &i, &d);
    fclose(fp);
    printf("Character: %c\nInteger: %d\nDouble: %lf\n", c, i, d);
}

EDIT: If you're looking for more info on fscanf, see here EDIT2:

Binary Solution

FILE *fp = fopen("C:\\example.txt", "rb");
if (fp)
{
    char buffer[sizeof(int) + sizeof(double) + sizeof(char)];
    if (fread(buffer, 1, sizeof(buffer), fp) == sizeof(buffer))
    {
        char c = *(char*)buffer;
        int i = *(int*)(buffer + sizeof(char));
        double d = *(double*)(buffer + sizeof(char) + sizeof(int));
    }
    fclose(fp);
}