Reading binary file without knowing file format

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2009
Posts: 5
Reputation: gmark is an unknown quantity at this point 
Solved Threads: 0
gmark gmark is offline Offline
Newbie Poster

Reading binary file without knowing file format

 
0
  #1
Apr 24th, 2009
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!!
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,621
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 121
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Reading binary file without knowing file format

 
0
  #2
Apr 24th, 2009
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:

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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: Reading binary file without knowing file format

 
0
  #3
Apr 24th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 5
Reputation: gmark is an unknown quantity at this point 
Solved Threads: 0
gmark gmark is offline Offline
Newbie Poster

Re: Reading binary file without knowing file format

 
0
  #4
Apr 24th, 2009
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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 5
Reputation: gmark is an unknown quantity at this point 
Solved Threads: 0
gmark gmark is offline Offline
Newbie Poster

Re: Reading binary file without knowing file format

 
0
  #5
Apr 24th, 2009
Originally Posted by nucleon View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: Reading binary file without knowing file format

 
0
  #6
Apr 24th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 5
Reputation: gmark is an unknown quantity at this point 
Solved Threads: 0
gmark gmark is offline Offline
Newbie Poster

Re: Reading binary file without knowing file format

 
0
  #7
Apr 24th, 2009
Originally Posted by nucleon View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: Reading binary file without knowing file format

 
0
  #8
Apr 24th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 5
Reputation: gmark is an unknown quantity at this point 
Solved Threads: 0
gmark gmark is offline Offline
Newbie Poster

Re: Reading binary file without knowing file format

 
0
  #9
Apr 24th, 2009
Originally Posted by nucleon View Post
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!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC