Vous êtes sur la page 1sur 10

C, howto read binary le into buer

http://www.linuxquestions.org/questions/programming-9/...

Register a domain and help support LQ

Home

Forums

HCL

Reviews

Tutorials

Articles

Register

Search

Main Menu

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming User Name User Name Password Remember Me? Log in

C, howto read binary file into buffer

Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.
Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community. You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today! Note that registered members see fewer ads, and ContentLink is completely disabled once you log in. Are you new to LinuxQuestions.org? Visit the following links: Site Howto | Site FAQ | Sitemap | Register Now If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here. Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Page 1 of 2 1 2 > LinkBack 04-21-2004, 03:17 PM Search this Thread #1 C, howto read binary file into buffer

Linux Forums Android Forum Search LQ Tags Linux HCL Linux Tutorials LQ Job Marketplace Linux Wiki Distro Reviews Book Reviews Download Linux Social Groups LQ Blogs Home (Con't)

Scrag
Member Registered: Mar 2004 Location: Wisconsin Distribution: Mandrake 9.2 Posts: 116

Could somebody provide a complete example of code that shows how to read a binary file into a buffer/array in C. Im trying to use fread() but my C book doesnt give very understandable examples. Or if theres something better than fread thats cool too.

1 of 10

Monday 03 September 2012 03:25 PM

C, howto read binary le into buer

http://www.linuxquestions.org/questions/programming-9/...

Rep:

Thank You!!

The connection has timed


[Log in to get rid of this advertisement]
04-21-2004, 03:35 PM #2

deiussum
Member Registered: Aug 2003 Location: Santa Clara, CA Distribution: Slackware Posts: 895 Rep:

This is just off the top of my head, so I may miss something, but here goes:
Code:

FILE *f; unsigned char buffer[MAX_FILE_SIZE]; int n; f = fopen("filename.bin", "rb"); if (f) { n = fread(buffer, MAX_FILE_SIZE, 1, f); } else { // error opening file } 1 members found this post helpful.

My LQ

Login Register
Write for LQ

04-21-2004, 04:09 PM

#3 Code:

The_Nerd
Member Registered: Aug 2002 Distribution: Debian Posts: 539 Rep:

void ReadFile(char *name) { FILE *file; char *buffer; unsigned long fileLen;

//Open file file = fopen(name, "rb"); if (!file) Main Menu { LQ Calendar fprintf(stderr, "Unable to open file %s", n return; LQ Rules } LQ Sitemap

LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.

2 of 10

Monday 03 September 2012 03:25 PM

C, howto read binary le into buer

http://www.linuxquestions.org/questions/programming-9/...

//Get file length fseek(file, 0, SEEK_END); fileLen=ftell(file); fseek(file, 0, SEEK_SET);

//Allocate memory buffer=(char *)malloc(fileLen+1); if (!buffer) { Syndicate fprintf(stderr, "Memory error!"); fclose(file); Latest Threads return; LQ News } Twitter: @linuxquestions //Read file contents into buffer 1 members found this post helpful.
04-21-2004, 04:18 PM #4

Site FAQ View New Posts View Latest Posts Zero Reply Threads LQ Wiki Most Wanted Jeremy's Blog Report LQ Bug

identi.ca: @linuxquestions Facebook: @linuxquestions

Scrag
Member Registered: Mar 2004 Location: Wisconsin Distribution: Mandrake 9.2 Posts: 116 Original Poster Rep:

Hhhhmmmmmm..... With your code, it reads in (im guessing) first 4 bytes of data, which is what is happening with the different code blocks that i've written. The file is ok, its a .bmp file. The output when printing buffer is: BM Where when I cat the file its: BM(* etc... etc....etc.........etc........ If im reading on the file right, and displaying it with correctly with printf("%s", buffer) , i should get lots of output to screen. I tried a for loop to read it in char by char, and output is still BM, and i kept an integer count with how many char's read, and its 4. Heres the code u gave me (modified).....any other ideas?? THANKS!! FILE *f; unsigned char buffer[10000]; int n; f = fopen("bmp.bmp", "rb"); n = fread(buffer, 10000, 1, f); printf("%s\n", &buffer);

#5

3 of 10

Monday 03 September 2012 03:25 PM

C, howto read binary le into buer

http://www.linuxquestions.org/questions/programming-9/...

04-21-2004, 04:26 PM

itsme86 You can't just print a binary buffer as a string. The reason is because if
Senior Member Registered: Jan 2004 Location: Oregon, USA Distribution: Slackware Posts: 1,246 Rep:

the binary data contains 8-bits in a row of 0's it's interpreted as a string-terminating NULL. So let's say your buffer looks like this:

011101010101010010000101000000000001111111010100010101100 The string interprets the 00000000 as the end of the string. If you want to show the entire contents of the buffer, then show each character one at a time until the counter equals the size of the file.
Code:

void dump_buffer(void *buffer, int buffer_size) { int i; for(i = 0;i < buffer_size;++i) printf("%c", ((char *)buffer)[i]); }

Last edited by itsme86; 04-21-2004 at 04:30 PM.

04-21-2004, 04:32 PM

#6

Scrag
Member Registered: Mar 2004 Location: Wisconsin Distribution: Mandrake 9.2 Posts: 116 Original Poster Rep:

COOL!!! It works now, thanks to all of you !!!!!

04-21-2004, 07:59 PM

#7

aluser
Member Registered: Mar 2004 Location: Massachusetts Distribution: Debian Posts: 557 Rep:

You can also use fwrite to print the whole thing with one function call -- it's similar to fread. (Use stdout as the FILE*)

04-21-2004, 08:58 PM

#8

deiussum
Member Registered: Aug 2003

Usually I find it's more useful to display binary data in a hexidecimal format. You can use something like:

4 of 10

Monday 03 September 2012 03:25 PM

C, howto read binary le into buer

http://www.linuxquestions.org/questions/programming-9/...

Location: Santa Clara, CA Distribution: Slackware Posts: 895 Rep:

Code:

for (int c=0;c<bufferSize;c++) { printf("%.2X ", (int)buffer[c]); // put an extra space between every 4 bytes if (c % 4 == 3) { printf(" "); } // Display 16 bytes per line if (c % 16 == 15) { printf("\n"); } } // Add an extra line feed for good measure printf("\n"); Of course, you can also add extra stuff in there to make it look more like a hex editor as well, but you get the idea...

01-16-2008, 09:34 AM

#9 Quote:

knockout_artist
Member Registered: Sep 2005 Distribution: fedora core 9 Posts: 324 Rep:

Originally Posted by The_Nerd


Code:

void ReadFile(char *name) { FILE *file; char *buffer; unsigned long fileLen; //Open file file = fopen(name, "rb"); if (!file) { fprintf(stderr, "Unable to open file %s" return; } //Get file length fseek(file, 0, SEEK_END); fileLen=ftell(file); fseek(file, 0, SEEK_SET); //Allocate memory buffer=(char *)malloc(fileLen+1); if (!buffer) { fprintf(stderr, "Memory error!"); fclose(file);

5 of 10

Monday 03 September 2012 03:25 PM

C, howto read binary le into buer

http://www.linuxquestions.org/questions/programming-9/...

return; }

Good Day, How do I read the buffer?? I have tried looping through it But I didn't any out put. Thanks.

09-23-2009, 12:38 AM

#10 How to Read Binary data file through C language

kishorworld
LQ Newbie Registered: Sep 2009 Posts: 1 Rep:

Hi friends.... m having a weather report file with extension as .wlk it's a formatted binary data file... Can anyone tell me that how to read this .wlk file and convert the data into the text format (.txt)with the help of C/C++ code ... if possible provide me source code or usefule links to create programme Email ID: kishor.j@ncmsl.com thanx in advance...
Last edited by kishorworld; 09-23-2009 at 12:39 AM. Reason: providing email id

09-23-2009, 12:51 AM

#11 Quote:

smeezekitty
Senior Member Registered: Sep 2009 Location: Washington U.S. Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS Posts: 2,119 Rep:

Originally Posted by kishorworld Hi friends.... m having a weather report file with extension as .wlk it's a formatted binary data file... Can anyone tell me that how to read this .wlk file and convert the data into the text format (.txt)with the help of C/C++ code ... if possible provide me source code or usefule links to create programme Email ID: kishor.j@ncmsl.com

6 of 10

Monday 03 September 2012 03:25 PM

C, howto read binary le into buer

http://www.linuxquestions.org/questions/programming-9/...

thanx in advance... your problem is more complicated please start your own thread

09-23-2009, 10:13 AM

#12

theNbomr
Senior Member Registered: Aug 2005 Distribution: OpenSuse, Fedora, Redhat, Debian Posts: 4,667 Blog Entries: 2 Rep:

Use the code posted by deiussum in article #8 of this thread. That won't interpret the file in any way, but simply display the bytes as hexadecimal. Interpretation of the data is application specific, and most people here won't know how to extract meaningful information from files of that type. For that, you will need some kind of published documentation, or a code library/API written for the purpose of interpreting the file. --- rod.

09-23-2009, 12:44 PM

#13

smeezekitty
Senior Member Registered: Sep 2009 Location: Washington U.S. Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS Posts: 2,119 Rep:

did you see how many views on this thread?! LOL

01-14-2010, 06:40 AM

#14 reading a .m4v file.

naphstor
LQ Newbie Registered: Jan 2010 Posts: 3 Rep:

hi all, i am a newbie in this forum. luckily i got the post i was searching for. but i have some issues. i have a .m4v file and want to read it and display its contents. i read the file and tried to print the buffer, but got some junk data on screen. i even tried the code given above for printing buffer, but effort seems to be in vain. The above modified code i used is :-

Code:

#include <stdio.h> main() { FILE *file; char *buffer; unsigned long fileLen;

7 of 10

Monday 03 September 2012 03:25 PM

C, howto read binary le into buer

http://www.linuxquestions.org/questions/programming-9/...

file = fopen("1.m4v", "rb"); if (!file) { fprintf(stderr, "can't open file %s", "1.m4 exit(1); } fseek(file, 0, SEEK_END); fileLen=ftell(file); fseek(file, 0, SEEK_SET); buffer=(char *)malloc(fileLen+1); if (!buffer) { fprintf(stderr, "Memory error!"); fclose(file); exit(1); }

please help me out. thanks.

01-14-2010, 08:24 AM

#15 Quote:

Sergei Steshenko
Senior Member Registered: May 2005 Posts: 4,168 Rep:

Originally Posted by naphstor hi all, i am a newbie in this forum. luckily i got the post i was searching for. but i have some issues. i have a .m4v file and want to read it and display its contents. i read the file and tried to print the buffer, but got some junk data on screen. i even tried the code given above for printing buffer, but effort seems to be in vain. The above modified code i used is :-

Code:

#include <stdio.h> main() { FILE *file; char *buffer; unsigned long fileLen; file = fopen("1.m4v", "rb"); if (!file) { fprintf(stderr, "can't open file %s", "1 exit(1); }

8 of 10

Monday 03 September 2012 03:25 PM

C, howto read binary le into buer

http://www.linuxquestions.org/questions/programming-9/...

fseek(file, 0, SEEK_END); fileLen=ftell(file); fseek(file, 0, SEEK_SET); buffer=(char *)malloc(fileLen+1); if (!buffer) { fprintf(stderr, "Memory error!"); fclose(file); exit(1); } fread(buffer, fileLen, 1, file);
please help me out. thanks.

What kind of data do you expect on the screen and why do you expect it to look not like junk ? I.e. do you know what data is displayed on screen as something meaningful and what data as junk/gibberish ? If I may, have you ever heard of ASCII ? If not, try to perform web search on ASCII and/or try man ascii on your UNIX/Linux bix.

Page 1 of 2 1 2 >

Posting Rules You You You You may may may may not not not not post new threads post replies post attachments edit your posts

9 of 10

Monday 03 September 2012 03:25 PM

C, howto read binary le into buer

http://www.linuxquestions.org/questions/programming-9/...

Posting Rules BB code is On Smilies are On [IMG] code is Off HTML code is Off Trackbacks are Off Pingbacks are On Refbacks are Off Forum Rules

Similar Threads
Thread read & write binary file error in redhat convert text file to binary excel file tcp/ip read and write buffer NULL buffer in read sys call unpredictable howto disable/correct frame buffer? Distorts boot screen... Thread Starter xcmore ust da_kidd_er jwstric2 BroX Forum Programming Linux - General Linux - Software Programming Debian Replies 11 2 0 3 3 Last Post 06-17-2005 07:48 AM 11-23-2004 02:33 AM 11-21-2004 04:13 PM 09-02-2004 07:13 PM 08-16-2004 03:02 AM

All times are GMT -5. The time now is 04:47 AM. Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member LQ Sitemap Open Source Consulting | Domain Registration

10 of 10

Monday 03 September 2012 03:25 PM

Vous aimerez peut-être aussi