943,769 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2879
  • C RSS
Apr 24th, 2009
0

Reading binary file without knowing file format

Expand Post »
Dear all,

I have read a lot topics about reading binary files but I didn't find something about my problem. How possible is it to read a binary file that you don't know the file format? I am sure that there are at least chars and float numbers. Thanks a lot!!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gmark is offline Offline
5 posts
since Apr 2009
Apr 24th, 2009
0

Re: Reading binary file without knowing file format

use "fopen()" to open the file, and specify "read, binary" as the mode.

  1. FILE * fp;
  2. fp = fopen("file.dat","rb");


note the following:

Quote ...
In the case of text files, depending on the environment where the application runs, some special character conversion may occur in input/output operations to adapt them to a system-specific text file format. In many environments, such as most UNIX-based systems, it makes no difference to open a file as a text file or a binary file; Both are treated exactly the same way, but differentiation is recommended for a better portability

--http://www.cplusplus.com/reference/clibrary/cstdio/fopen/
in other words, if you think the file has numeric values, read them the same way using fgets() or sscanf() or similar.


.
Last edited by jephthah; Apr 24th, 2009 at 1:25 pm.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Apr 24th, 2009
0

Re: Reading binary file without knowing file format

It's quite a challenge decoding a binary file with an unkown format! In general, it's not possible. How do you know if the first two bytes are the characters AB or the 16-bit integer with decimal value 1106 (assuming an ascii representation)? You might want to view the file in a hex/ascii viewer.
Reputation Points: 163
Solved Threads: 91
Posting Pro in Training
nucleon is offline Offline
476 posts
since Oct 2008
Apr 24th, 2009
0

Re: Reading binary file without knowing file format

Thanks for your answer, I have the following code, I will check also your link, I use linux but I can't see an text that should be in the binary.

  1. #include<stdio.h>
  2.  
  3. int main() {
  4. FILE *file;
  5. unsigned char *buffer;
  6. int i;
  7. unsigned long fileLen;
  8.  
  9. //Open file
  10. file = fopen("TRACE.0000002958000001000000.mpit", "rb");
  11. if (!file)
  12. {
  13. fprintf(stderr, "Unable to open file %s", "test.mpit");
  14. return;
  15. }
  16.  
  17. //Get file length
  18. fseek(file, 0, SEEK_END);
  19. fileLen=ftell(file);
  20. fseek(file, 0, SEEK_SET);
  21.  
  22. //Allocate memory
  23. buffer=(unsigned char *)malloc((fileLen+1)*sizeof(unsigned char));
  24. if (!buffer)
  25. {
  26. fprintf(stderr, "Memory error!");
  27. fclose(file);
  28. return;
  29. }
  30.  
  31. //Read file contents into buffer
  32. fread(buffer, fileLen, 1, file);
  33. fclose(file);
  34.  
  35. int ii;
  36. for(ii = 0;ii < fileLen;++ii)
  37. printf("%d, %c n",ii, ((unsigned char *)buffer)[ii]);
  38. free(buffer);
  39. printf("%d \n",fileLen);
  40. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gmark is offline Offline
5 posts
since Apr 2009
Apr 24th, 2009
0

Re: Reading binary file without knowing file format

Click to Expand / Collapse  Quote originally posted by nucleon ...
It's quite a challenge decoding a binary file with an unkown format! In general, it's not possible. How do you know if the first two bytes are the characters AB or the 16-bit integer with decimal value 1106 (assuming an ascii representation)? You might want to view the file in a hex/ascii viewer.
Yes I agree it's difficult. I suppose that the file has data like :

  1. ----------------------------------------------------------------
  2. MPI Routine #calls avg. bytes time(sec)
  3. ----------------------------------------------------------------
  4. MPI_Comm_size 2 0.0 0.000
  5. MPI_Comm_rank 1 0.0 0.000
  6. MPI_Send 16827 4714.6 0.067
  7. MPI_Recv 16833 4810.6 0.483
  8. MPI_Probe 6 0.0 0.000
  9. MPI_Bcast 201 7.4 0.013
  10. MPI_Barrier 1 0.0 0.000
  11. MPI_Gather 2 4.0 0.000
  12. MPI_Gatherv 3 23240.0 0.013
  13. MPI_Reduce 300 9.3 0.818
  14. MPI_Allreduce 504 13.5 0.028
  15. ----------------------------------------------------------------
  16. total communication time = 1.422 seconds.
  17. total elapsed time = 24.526 seconds.
  18. user cpu time = 24.408 seconds.
  19. system time = 0.030 seconds.
  20. maximum memory size = 15428 KBytes.
  21. ----------------------------------------------------------------

It is free program and it was producing text files but now they change it to produce binary files in order for another program to reads the files but I don't want the other program.
Last edited by gmark; Apr 24th, 2009 at 2:01 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gmark is offline Offline
5 posts
since Apr 2009
Apr 24th, 2009
0

Re: Reading binary file without knowing file format

So you basically want to write a program that will translate the new format binary file into the old format text file?
If it's a free program, can you get the source?
Or can you find a description of its binary format online?
Reputation Points: 163
Solved Threads: 91
Posting Pro in Training
nucleon is offline Offline
476 posts
since Oct 2008
Apr 24th, 2009
0

Re: Reading binary file without knowing file format

Click to Expand / Collapse  Quote originally posted by nucleon ...
So you basically want to write a program that will translate the new format binary file into the old format text file?
If it's a free program, can you get the source?
Or can you find a description of its binary format online?
Yes that's exactly. It's free but they give only executable file and there is no documentation about the binary file. I suppose that the binary file format is the same with the text. I sent them an email to ask about it, but as they give another program with license (free of charge) I don't know if they will give me any information. We just don't want to use any license. The other solution is to change the program but this is the easiest and we try first this. So as I see without file format maybe there is no solution.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gmark is offline Offline
5 posts
since Apr 2009
Apr 24th, 2009
0

Re: Reading binary file without knowing file format

You may be able to figure it out with some detective work. Try running the old program and the new program in such a way that they should produce the same data. Then compare the files (viewing the binary file in a hex editor). But they could have added some things or moved some things so it could be difficult to compare them. If you happen to do the above (generate the "equivalent" text and binary files), and they're not too big, zip and attach them.
Reputation Points: 163
Solved Threads: 91
Posting Pro in Training
nucleon is offline Offline
476 posts
since Oct 2008
Apr 24th, 2009
0

Re: Reading binary file without knowing file format

Click to Expand / Collapse  Quote originally posted by nucleon ...
You may be able to figure it out with some detective work. Try running the old program and the new program in such a way that they should produce the same data. Then compare the files (viewing the binary file in a hex editor). But they could have added some things or moved some things so it could be difficult to compare them. If you happen to do the above (generate the "equivalent" text and binary files), and they're not too big, zip and attach them.
I don't have the old version and I can't find it anywhere! I sent an email to an author's report that I have read about text files version but there isn't such email any more, thanks for the help I will try to see if the binary files are created right because at least I should see the text about some commands so I believe something maybe is wrong about the creation of the files. If I have another question I will write here, thanks!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gmark is offline Offline
5 posts
since Apr 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Pointer Error in c
Next Thread in C Forum Timeline: Help with Win32 API Menu





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC