P. 1
fread

fread

|Views: 3|Likes:
Published by Shubhra Sinha

More info:

Published by: Shubhra Sinha on Oct 09, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

10/09/2012

pdf

text

original

C, howto read binary file into buffer

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

Articles. if (f) { n = fread(buffer. howto read binary file into buffer http://www. f = fopen("filename. "Unable to open file %s". if (!file) Main Menu { LQ Calendar fprintf(stderr.org is looking for people interested in writing Editorials. unsigned long fileLen. My LQ Login Register Write for LQ 04-21-2004. so I may miss something. 1. LQ Rules } LQ Sitemap LinuxQuestions. MAX_FILE_SIZE. CA Distribution: Slackware Posts: 895 Rep: This is just off the top of my head. 2 of 10 Monday 03 September 2012 03:25 PM . //Open file file = fopen(name. f).org/questions/programming-9/. but here goes: Code: FILE *f. and more. unsigned char buffer[MAX_FILE_SIZE]. 04:09 PM #3 Code: The_Nerd Member Registered: Aug 2002 Distribution: Debian Posts: 539 Rep: void ReadFile(char *name) { FILE *file.. Rep: Thank You!! The connection has timed [Log in to get rid of this advertisement] 04-21-2004.linuxquestions. "rb").C. n return. int n. } else { // error opening file } 1 members found this post helpful. If you'd like to contribute content. "rb").. Reviews. let us know. 03:35 PM #2 deiussum Member Registered: Aug 2003 Location: Santa Clara.bin". char *buffer.

linuxquestions. printf("%s\n".bmp".. "rb"). //Get file length fseek(file. it reads in (im guessing) first 4 bytes of data.bmp file. The output when printing buffer is: BM¶ Where when I cat the file its: BM¶(*€ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ etc.. 0. fileLen=ftell(file).2 Posts: 116 Original Poster Rep: Hhhhmmmmmm. Heres the code u gave me (modified). #5 3 of 10 Monday 03 September 2012 03:25 PM .C. 04-21-2004. int n. howto read binary file into buffer http://www. and its 4. etc..etc......... LQ News } Twitter: @linuxquestions //Read file contents into buffer 1 members found this post helpful.. &buffer). if (!buffer) { Syndicate fprintf(stderr. The file is ok..ca: @linuxquestions Facebook: @linuxquestions Scrag Member Registered: Mar 2004 Location: Wisconsin Distribution: Mandrake 9. n = fread(buffer.. SEEK_END)... f). fseek(file. buffer) .org/questions/programming-9/.. i should get lots of output to screen..any other ideas?? THANKS!! FILE *f. and i kept an integer count with how many char's read. 1. 0. fclose(file). and displaying it with correctly with printf("%s".. unsigned char buffer[10000].. f = fopen("bmp... 10000.. If im reading on the file right... Latest Threads return. I tried a for loop to read it in char by char. and output is still BM¶.. 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.. //Allocate memory buffer=(char *)malloc(fileLen+1). With your code. which is what is happening with the different code blocks that i've written. its a .. "Memory error!")..etc. SEEK_SET)..

The reason is because if Senior Member Registered: Jan 2004 Location: Oregon. If you want to show the entire contents of the buffer.linuxquestions. for(i = 0. 04:32 PM #6 Scrag Member Registered: Mar 2004 Location: Wisconsin Distribution: Mandrake 9.i < buffer_size.++i) printf("%c". Code: void dump_buffer(void *buffer.org/questions/programming-9/. int buffer_size) { int i. So let's say your buffer looks like this: 011101010101010010000101000000000001111111010100010101100 The string interprets the 00000000 as the end of the string. } Last edited by itsme86. You can use something like: 4 of 10 Monday 03 September 2012 03:25 PM .2 Posts: 116 Original Poster Rep: COOL!!! It works now. 04-21-2004 at 04:30 PM.C. 04:26 PM itsme86 You can't just print a binary buffer as a string. 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. howto read binary file into buffer http://www... 08:58 PM #8 deiussum Member Registered: Aug 2003 Usually I find it's more useful to display binary data in a hexidecimal format. 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 -. 04-21-2004. (Use stdout as the FILE*) 04-21-2004. ((char *)buffer)[i]).it's similar to fread. 04-21-2004. thanks to all of you !!!!! 04-21-2004. then show each character one at a time until the counter equals the size of the file.

. 0. Of course.org/questions/programming-9/. fileLen=ftell(file). "Unable to open file %s" return. but you get the idea.c<bufferSize. "Memory error!"). (int)buffer[c]). 0. fseek(file. SEEK_SET). Location: Santa Clara. CA Distribution: Slackware Posts: 895 Rep: Code: for (int c=0. if (!file) { fprintf(stderr. 01-16-2008. "rb"). unsigned long fileLen. fclose(file). you can also add extra stuff in there to make it look more like a hex editor as well. //Allocate memory buffer=(char *)malloc(fileLen+1). 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. } } // Add an extra line feed for good measure printf("\n"). 5 of 10 Monday 03 September 2012 03:25 PM ..linuxquestions. // put an extra space between every 4 bytes if (c % 4 == 3) { printf(" ").2X ". howto read binary file into buffer http://www.. } //Get file length fseek(file.C.c++) { printf("%. char *buffer. SEEK_END). //Open file file = fopen(name. if (!buffer) { fprintf(stderr.. } // Display 16 bytes per line if (c % 16 == 15) { printf("\n").

My own OS Posts: 2..wlk it's a formatted binary data file. return. Can anyone tell me that how to read this .S. Reason: providing email id 09-23-2009.wlk file and convert the data into the text format (..j@ncmsl.. 09-23-2009... m having a weather report file with extension as . Distribution: Damn Small Linux. M$ Ickdows Vista... m having a weather report file with extension as .linuxquestions.. 12:38 AM #10 How to Read Binary data file through C language kishorworld LQ Newbie Registered: Sep 2009 Posts: 1 Rep: Hi friends.wlk file and convert the data into the text format (. if possible provide me source code or usefule links to create programme Email ID: kishor.. if possible provide me source code or usefule links to create programme Email ID: kishor.org/questions/programming-9/..j@ncmsl. Can anyone tell me that how to read this ... } Good Day. howto read binary file into buffer http://www. 12:51 AM #11 Quote: smeezekitty Senior Member Registered: Sep 2009 Location: Washington U.. Thanks..... 09-23-2009 at 12:39 AM. How do I read the buffer?? I have tried looping through it But I didn't any out put.wlk it's a formatted binary data file.txt)with the help of C/C++ code . Last edited by kishorworld. KateOs.com thanx in advance..txt)with the help of C/C++ code .com 6 of 10 Monday 03 September 2012 03:25 PM .C.119 Rep: Originally Posted by kishorworld Hi friends.

i read the file and tried to print the buffer. or a code library/API written for the purpose of interpreting the file. --. thanx in advance.m4v file. That won't interpret the file in any way.667 Blog Entries: 2 Rep: Use the code posted by deiussum in article #8 of this thread.rod. Redhat.m4v file and want to read it and display its contents. 7 of 10 Monday 03 September 2012 03:25 PM . Interpretation of the data is application specific. The above modified code i used is :- Code: #include <stdio. My own OS Posts: 2. M$ Ickdows Vista. naphstor LQ Newbie Registered: Jan 2010 Posts: 3 Rep: hi all. i have a .linuxquestions. KateOs. i am a newbie in this forum.. Distribution: Damn Small Linux. char *buffer. your problem is more complicated please start your own thread 09-23-2009. Fedora.h> main() { FILE *file. For that. 10:13 AM #12 theNbomr Senior Member Registered: Aug 2005 Distribution: OpenSuse. howto read binary file into buffer http://www. you will need some kind of published documentation.C. luckily i got the post i was searching for.. i even tried the code given above for printing buffer. unsigned long fileLen. but simply display the bytes as hexadecimal.org/questions/programming-9/. but i have some issues. 06:40 AM #14 reading a . but effort seems to be in vain.. Debian Posts: 4. 12:44 PM #13 smeezekitty Senior Member Registered: Sep 2009 Location: Washington U.. but got some junk data on screen.S. 09-23-2009.119 Rep: did you see how many views on this thread?! LOL 01-14-2010. and most people here won't know how to extract meaningful information from files of that type.

file = fopen("1.. 0. "Memory error!").m4v".org/questions/programming-9/. i have a . "1 exit(1). fclose(file). fileLen=ftell(file). if (!file) { fprintf(stderr. but got some junk data on screen. thanks. char *buffer. "can't open file %s".168 Rep: Originally Posted by naphstor hi all. buffer=(char *)malloc(fileLen+1). "1. "can't open file %s". 01-14-2010.m4v". luckily i got the post i was searching for. i am a newbie in this forum.C. 0.linuxquestions. } please help me out. SEEK_END). "rb").m4 exit(1). if (!buffer) { fprintf(stderr. } fseek(file. } 8 of 10 Monday 03 September 2012 03:25 PM . fseek(file. but i have some issues. file = fopen("1. SEEK_SET). but effort seems to be in vain. 08:24 AM #15 Quote: Sergei Steshenko Senior Member Registered: May 2005 Posts: 4. exit(1).h> main() { FILE *file.. i read the file and tried to print the buffer. howto read binary file into buffer http://www. unsigned long fileLen. i even tried the code given above for printing buffer. The above modified code i used is :- Code: #include <stdio. if (!file) { fprintf(stderr. "rb").m4v file and want to read it and display its contents.

fseek(file. What kind of data do you expect on the screen and why do you expect it to look not like junk ? I. exit(1). buffer=(char *)malloc(fileLen+1). if (!buffer) { fprintf(stderr. 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 . try to perform web search on ASCII and/or try man ascii on your UNIX/Linux bix. have you ever heard of ASCII ? If not. thanks. fileLen.e.linuxquestions.C. } fread(buffer. SEEK_SET). fileLen=ftell(file). do you know what data is displayed on screen as something meaningful and what data as junk/gibberish ? If I may. fclose(file).. please help me out. howto read binary file into buffer http://www. file). 0. "Memory error!"). fseek(file. SEEK_END).org/questions/programming-9/.. 1. 0.

Rules .Donations .Contributing Member LQ Sitemap Open Source Consulting | Domain Registration 10 of 10 Monday 03 September 2012 03:25 PM .. Thread Starter xcmore ust da_kidd_er jwstric2 BroX Forum Programming Linux ..C. howto read binary file into buffer http://www. The time now is 04:47 AM.linuxquestions. Contact Us .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..General Linux .. 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.Advertising Info .org/questions/programming-9/.LQ Merchandise .

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->