Because i know what this data contain (this data begin every 512 bytes with the length of the packet). but it's not so important.Why do you think the first few bytes indicate the length of the data?
Thanks.
Because i know what this data contain (this data begin every 512 bytes with the length of the packet). but it's not so important.Why do you think the first few bytes indicate the length of the data?
Thanks.
If you run this program, you can check it's output by opening a console window, and typing:
fc file_data copy1 <enter>
Windows will compare the two files and print any differences, or the message:
"no differences encountered"
as it does here.
Most of the file's contents won't display, of course.Code:#include <stdio.h> #include <stdlib.h> long filesize(FILE *fp); int main() { int n; long num, i; FILE *fp; char *buff; fp=fopen("file_data", "rb"); //short filename is "file_d~1" if(fp==NULL) { printf("Error opening the file"); return 1; } printf("\n\n"); num=filesize(fp); printf("Filesize of file_data is %ld bytes\n", num); buff=malloc(num); if(buff==NULL) { printf("Error allocating memory"); return 1; } n = fread(buff, num, 1, fp); if(n==0) { printf("Error reading the file"); return 1; } /* just shows another way of printing out the data - many char's are unprintable */ /* for(i=0;i<num;i++) { fputc(buff[i], stdout); if(i % 4 == 0 && i) printf(" "); } */ fclose(fp); fp=fopen("copy1", "wb"); if(fp==NULL) { printf("Error opening copy1 file"); return 1; } fwrite(buff, num, 1, fp); fclose(fp); printf("\n\n\t\t\t press enter when ready"); i = getchar(); ++i; return 0; } long filesize(FILE *stream) { long curpos, length; curpos = ftell(stream); fseek(stream, 0L, SEEK_END); length = ftell(stream); fseek(stream, curpos, SEEK_SET); return length; }
An interesting error for me, was forgetting that sizeof(buff) stays at 2 bytes, even after it's been malloc'd to a much larger size - but that's the thing - buff hasn't been made bigger - it just has been given the address of a place in memory that is set aside for it (and it now has the address of), and that place is now bigger.
Last edited by Adak; 09-01-2010 at 06:44 AM.
OK, so I am trying to help figure out how the length of the packet is stored in the binary data using your example. So what format is the length of the packet given in? Or is the data a fixed length structure of 512 bytes?
If the data begins every 512 bytes and thus if the longest packet size is 511 or 0x01FF, then you need at least 9 bits to give the length if all lengths are possible. However, your sample file begins with 0x07FF which is larger than 512. If it is little endian it is 0xFF07 which is still larger than 512. Your sample in message 1 begins with 0x0500 which is also larger that 512. If it is little endian, then it is 0x0005. This is less than 512.
Do you see where I am going? I am trying to use your to samples (an very small set) to try to correctly interpret the format.
Or are you just trying to print your array in hexadecimal format? If so, look at the (f or s) printf formats %x or %X.
Last edited by pheininger; 09-01-2010 at 07:06 AM.
Thanks Adak for the reply, but your program display the size of the file i don't want this information, i know it!
I will explain again my problem, suppose i have a binary file (file_data), under unix like systems i can display the contents of this file using "od -x" command for example, this is a sample of this output:
I want to read this file and store this hex dump in a char buffer to do some parsing stuff. is this clear?Code:$od -x file_data 0000000 ff07 ffff ffff ffff ffff ffff ffff ffff 0000020 06ff 1016 0740 fff3 ffff ffff ffff ffff 0000040 6004 0101 3074 ffff ffff ffff 4bff 4200 0000060 331f 0000 0051 fff1 ffff 4bff 4200 011f 0000100 ff04 00ff 0011 0000 ff00 1610 1939 1202 0000120 2009 3926 0219 0912 2920 1939 1202 2009 0000140 0000 0000 0003 0000 0000 0003 0000 0000 0000160 0000 0000 ff01 ffff ffff ffff ffff ffff 0000200 ffff 05ff 0505 0605 1605 4010 f307 ffff 0000220 ffff ffff 00ff 4132 02c4 0300 0010 0001 0000240 0000 ffff ffff ffff ffff ffff ffff ffff 0000260 ffff ffff 0000 0000 0000 0000 0000 0000 0000300 0000 0000 00ff 0000 ffff ffff ffff ffff 0000320 ffff ffff 00ff 05ff 08ff 01f0 08ff 01f0 0000340 ffff ffff ffff ffff 02ff 3915 0219 0912 0000360 ff20 5343 5049 3230 2020 ffff ffff ffff 0000400 ffff 0506 5917 0078 8000 f200 ffff ffff 0000420 0000 00db 1412 0021 0000 60c6 4132 02c4
Regards.
thanks pheininger for the reply,
I want just to read the hex dump of this file and store it as it will diplayed with a command like "od -x" in a big char buffer and then i will use some pointers to do some parsing stufs.
Regards.
So instead of displaying the data you want, pipe that output right to a file.
You don't want to work with the binary data. You want to work with the hex representation of the data. <light bulb moment>
Then you can use a program like the one I posted, and you'll have the data, in the buffer.
Last edited by Adak; 09-01-2010 at 07:27 AM.
Hi Adak,
your program does not seem to do what i want it just copy the file and display his size!.
Regards.
The key thing is that it takes a file, gets the size of the file, and makes the buffer exactly that size. Then it puts everything from the file, into the buffer so you can then do whatever you want to do with it.
If you don't want to see the file size displayed, just // it out.
Remember to open the output of the hex dump file though, NOT the original data file.
Ok but the main problem is how to bufferize(store in a buffer) the data of the file as a string (hex dump) so i can manipulate them more simply.
Example:
suppose the hex dump of my binary file is :
i want to store it in a char buffer:Code:0000000 ff07 ffff ffff ffff ffff ffff ffff ffff 0000020 06ff 1016 0740 fff3 ffff ffff ffff ffff 0000040 6004 0101 3074 ffff ffff ffff 4bff 4200 0000060 331f 0000 0051 fff1 ffff 4bff 4200 011f 0000100 ff04 00ff 0011 0000 ff00 1610 1939 1202
char buffer[512];
and after running the program i can loop through my data file using this char buffer.
Regards.
First, do you know how to pipe the output of the hex dump, into a file?
On DOS and Windows, I use >, as in:
dir *.* >fileNames.txt
Which puts the entire directory into a new file named fileNames.txt.
That's using a pipe, the > char.
I don't want to use pipe or commands i receive the file from the network, and i want to parse it in a real time manner, so pipe is not a good idea(like all scripting) in a real time environnement.
Thanks.
I believe this is most of what you want. Check it out:
(still has a lot of stuff I didn't remove, but you can do that)
Code:#include <stdio.h> #include <stdlib.h> long filesize(FILE *fp); int main() { int j, n; long num, i; FILE *fp; char *buff; char *buff2; fp=fopen("file_data", "rb"); //short filename is "file_d~1" if(fp==NULL) { printf("Error opening the file"); return 1; } printf("\n\n"); num=filesize(fp); printf("Filesize of file_data is %ld bytes\n", num); buff=malloc(num); buff2=malloc(num); if(buff==NULL) { printf("Error allocating memory"); return 1; } n = fread(buff, num, 1, fp); if(n==0) { printf("Error reading the file"); return 1; } printf("\n"); for(j=0;j<num;j++) { if(j % 8==0) { printf("\n\t"); } sprintf(buff2+j,"%x",buff[j]); printf("%x\t", buff[j]); } /* just shows another way of printing out the data - many char's are unprintable */ /* for(i=0;i<num;i++) { fputc(buff[i], stdout); if(i % 4 == 0 && i) printf(" "); } */ fclose(fp); fp=fopen("copy1", "wb"); if(fp==NULL) { printf("Error opening copy1 file"); return 1; } fwrite(buff, num, 1, fp); fclose(fp); free(buff); free(buff2); printf("\n\n\t\t\t press enter when ready"); i = getchar(); ++i; return 0; } long filesize(FILE *stream) { long curpos, length; curpos = ftell(stream); fseek(stream, 0L, SEEK_END); length = ftell(stream); fseek(stream, curpos, SEEK_SET); return length; }
Last edited by Adak; 09-01-2010 at 09:37 AM.
Thanks Adak for the code,
i think i need more help fix my program.... i don't understand why you want to create a copy file? i don't want this.
Thanks again.
Regards.
What kind of parsing stuff do you want to do? I have drafted (but not tested) code which will read the binary file into a linked list of lines that are the equivalent of the output of the "od -x" command.
If you just want it in a big character buffer, what is the biggest file you will want to process? For example, your 4KB example file when converted would be about (8*8+11/line)*256lines or about 19200 characters. Is this really what you want to manipulate?
Are you sure you do not want to work the raw data in either bytes or words (16bit quantity) or the raw data converted to printable hex digits without whitespace. With the "od -x" format, you will have to remove (or ignore) the address part of the line or the white space between words .
Please explain what operations you are going to do next with the data so that we could make some reasonable suggestions.