Binary_data

Hi everyone,

I would like to open a binary data file, read and print the information using c/c++.
In Matlab I can use fread to read the file, but I am not sure how to use fread in c/c++.

Here is my code in Matlab:
file=fopen(MyBinaryfile,'rb');
V =fread(file,1,'*int32');
E =fread(file,1,'*double');
p =fread(file,1,'*int32');
nrP =fread(file,1,'*int64');
nrSh =fread(file,1,'*int64');
nrPr =fread(file,1,'*int64');

Here is my code in c:

#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *ptr1;
ptr1 = fopen("MyBinaryfile","rb");

?

fclose(ptr1);
return 0;
}

Many thanks

Lalageh
Hi,

You can read your file char by char or string:
1
2
3
4
5
6
7
8
9
10
if ( ptr1 != NULL )
   {
          char line[80];          /* or other suitable maximum line size */
          
          while ( fgets ( line, sizeof line, ptr1 ) != NULL ) /* read a line */
          {
              fputs ( line, stdout ); /* write the line */
          }
          fclose ( file );
   }
Last edited on
Thank you very much for your help it works, but I was wondering if there is more efficient way so I would avoid loops?

My files are huge ~10 GB, it will take long time to run.

Thank you in advance
Topic archived. No new replies allowed.