0

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!

4
  • 1
    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". Commented Apr 12, 2012 at 6:06
  • 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 Commented Apr 12, 2012 at 6:10
  • 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. Commented Apr 12, 2012 at 6:19
  • Does it make sense to count lines for a binary file? Commented Apr 12, 2012 at 6:44

2 Answers 2

0

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.