| | |
Reading binary file without knowing file format
![]() |
use "fopen()" to open the file, and specify "read, binary" as the mode.
note the following:
in other words, if you think the file has numeric values, read them the same way using fgets() or sscanf() or similar.
.
c Syntax (Toggle Plain Text)
FILE * fp; 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/
.
Last edited by jephthah; Apr 24th, 2009 at 1:25 pm.
•
•
Join Date: Apr 2009
Posts: 5
Reputation:
Solved Threads: 0
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.
C Syntax (Toggle Plain Text)
#include<stdio.h> int main() { FILE *file; unsigned char *buffer; int i; unsigned long fileLen; //Open file file = fopen("TRACE.0000002958000001000000.mpit", "rb"); if (!file) { fprintf(stderr, "Unable to open file %s", "test.mpit"); return; } //Get file length fseek(file, 0, SEEK_END); fileLen=ftell(file); fseek(file, 0, SEEK_SET); //Allocate memory buffer=(unsigned char *)malloc((fileLen+1)*sizeof(unsigned char)); if (!buffer) { fprintf(stderr, "Memory error!"); fclose(file); return; } //Read file contents into buffer fread(buffer, fileLen, 1, file); fclose(file); int ii; for(ii = 0;ii < fileLen;++ii) printf("%d, %c n",ii, ((unsigned char *)buffer)[ii]); free(buffer); printf("%d \n",fileLen); }
•
•
Join Date: Apr 2009
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
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.
C Syntax (Toggle Plain Text)
---------------------------------------------------------------- MPI Routine #calls avg. bytes time(sec) ---------------------------------------------------------------- MPI_Comm_size 2 0.0 0.000 MPI_Comm_rank 1 0.0 0.000 MPI_Send 16827 4714.6 0.067 MPI_Recv 16833 4810.6 0.483 MPI_Probe 6 0.0 0.000 MPI_Bcast 201 7.4 0.013 MPI_Barrier 1 0.0 0.000 MPI_Gather 2 4.0 0.000 MPI_Gatherv 3 23240.0 0.013 MPI_Reduce 300 9.3 0.818 MPI_Allreduce 504 13.5 0.028 ---------------------------------------------------------------- total communication time = 1.422 seconds. total elapsed time = 24.526 seconds. user cpu time = 24.408 seconds. system time = 0.030 seconds. maximum memory size = 15428 KBytes. ----------------------------------------------------------------
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.
•
•
Join Date: Apr 2009
Posts: 5
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Apr 2009
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
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.
![]() |
Similar Threads
- File Format Conversion/Compression (C)
- memory management in wndows 2000 (Windows NT / 2000 / XP)
Other Threads in the C Forum
- Previous Thread: Const volatile
- Next Thread: Help with Win32 API Menu
Views: 1503 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C
api array arrays bash behaviour binarysearch bind c++ calculator char character copyanyfile copypdffile createprocess() database directory drawing dynamic error exec feet file floatingpointvalidation fork free function functions getlogicaldrivestrin givemetehcodez global grade graphics homework i/o ide infiniteloop initialization input insert interest kilometer lazy leet_speak license linked linkedlist linux list matrix meter microsoft mysql no-effort odf open openwebfoundation operator output path pause pdf pointer pointers posix power problem processing program programming pyramidusingturboccodes read recursion recv recvblocked segmentationfault single socketprogramming spoonfeeding sql standard strchr string strtok structures student studio suggestions system telephone test turbo-c turboc undefined unix urboc user why-not-to-use-turbo win32api winxp _getdelim






