fread
TRANSCRIPT
-
7/31/2019 fread
1/10
Register a domain and help support LQ
Home Forums HCL Reviews Tutorials Articles Register Search
LinuxQuestions.org > Forums > Non-*NIX
Forums > Programming
C, howto read binary file intobuffer
User Name User Name Remember Me?
Password Log in
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 havethe 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-relatedcookies.
Page 1 of 2 1 2 >
LinkBack Search this Thread
04-21-2004, 03:17 PM # 1
ScragMember
Registered: Mar 2004Location: WisconsinDistribution: Mandrake 9.2Posts: 116
C, howto read binary file into buffer
Could somebody provide a complete example of codethat shows how to read a binary file into a buffer/arrayin C. Im trying to use fread() but my C book doesntgive very understandable examples. Or if theressomething better than fread thats cool too.
Main Menu
Linux ForumsAndroid ForumSearchLQ TagsLinux HCLLinux TutorialsLQ Job MarketplaceLinux WikiDistro ReviewsBook ReviewsDownload LinuxSocial GroupsLQ BlogsHome
(Con't)
wto read binary le into buffer http://www.linuxquestions.org/questions/programming-9/...
0 Monday 03 September 2012 03:25 PM
-
7/31/2019 fread
2/10
Theconnectionhas timed
[ Log in to get rid of this advertisement]
Rep:
ThankYou!!
04-21-2004, 03:35 PM # 2
deiussumMember
Registered: Aug 2003Location: Santa Clara, CADistribution: SlackwarePosts: 895
Rep:
This is just off the top of my head, so I may misssomething, 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.
04-21-2004, 04:09 PM # 3
The_NerdMember
Registered: Aug 2002Distribution: DebianPosts: 539
Rep:
Code:
void ReadFile(char *name){
FILE *file;char *buffer;unsigned long fileLen;
//Open filefile = fopen(name, "rb");if (!file){
fprintf(stderr, "Unable toreturn;
}
My LQ
LoginRegister
Write for LQ
LinuxQuestions.org islooking for peopleinterested in writingEditorials, Articles,Reviews, and more. If you'd like tocontribute content, letus know .
Main Menu
LQ CalendarLQ RulesLQ Sitemap
wto read binary le into buffer http://www.linuxquestions.org/questions/programming-9/...
0 Monday 03 September 2012 03:25 PM
-
7/31/2019 fread
3/10
//Get file lengthfseek(file, 0, SEEK_END);fileLen=ftell(file);fseek(file, 0, SEEK_SET);
//Allocate memorybuffer=(char *)malloc(fileLen+1);if (!buffer){
fprintf(stderr, "Memory efclose(fil
return;}
1 members found this post helpful.
04-21-2004, 04:18 PM # 4
ScragMember
Registered: Mar 2004Location: WisconsinDistribution: Mandrake 9.2Posts: 116
Original PosterRep:
Hhhhmmmmmm.....
With your code, it reads in (im guessing) first 4 bytes of data, which is what is happening with the different codeblocks 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 withcorrectly with printf("%s", buffer) , i should get lots of output to screen. I tried a for loop to read it in char bychar, and output is still BM, and i kept an integer countwith how many char's read, and its 4.Heres the code u gave me (modified).....any otherideas??
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
Site FAQView New PostsView Latest PostsZero Reply ThreadsLQ Wiki MostWantedJeremy's BlogReport LQ Bug
Syndicate
Latest ThreadsLQ News
Twitter: @linuxquestionsidenti.ca: @linuxquestionsFacebook: @linuxquestions
wto read binary le into buffer http://www.linuxquestions.org/questions/programming-9/...
0 Monday 03 September 2012 03:25 PM
-
7/31/2019 fread
4/10
04-21-2004,04:26 PM
itsme86SeniorMember
Registered:Jan 2004Location:
Oregon,USADistribution:SlackwarePosts: 1,246
Rep:
You can't just print a binary buffer as a string. The reason is because if the binary data contains 8-bits in a row of 0's it's interpreted as astring-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 eachcharacter 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
ScragMember
Registered: Mar 2004
Location: WisconsinDistribution: Mandrake 9.2Posts: 116
Original PosterRep:
COOL!!!
It works now, thanks to all of you !!!!!
04-21-2004, 07:59 PM # 7
aluserMember
Registered: Mar 2004Location: MassachusettsDistribution: DebianPosts: 557
Rep:
You can also use fwrite to print the whole thing with onefunction call -- it's similar to fread. (Use stdout as theFILE*)
04-21-2004, 08:58 PM # 8
deiussumMember
Registered: Aug 2003
Usually I find it's more useful to display binary data in ahexidecimal format. You can use something like:
wto read binary le into buffer http://www.linuxquestions.org/questions/programming-9/...
0 Monday 03 September 2012 03:25 PM
-
7/31/2019 fread
5/10
Location: Santa Clara, CADistribution: SlackwarePosts: 895
Rep:
Code:
for (int c=0;c
-
7/31/2019 fread
6/10
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
kishorworldLQ Newbie
Registered: Sep 2009Posts: 1
Rep:
How to Read Binary data file through C language
Hi friends....
m having a weather report file with extension as .wlkit's a formatted binary data file...
Can anyone tell me that how to read this .wlk file andconvert the data into the text format (.txt)with the helpof C/C++ code ...
if possible provide me source code or usefule links tocreate programme
Email ID: [email protected]
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
smeezekittySenior Member
Registered: Sep 2009Location: Washington U.S.Distribution: Damn SmallLinux, KateOs, M$ Ickdows
Vista, My own OSPosts: 2,119
Rep:
Quote:
Originally Posted by kishorworldHi 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 usefulelinks to create programme
Email ID: [email protected]
wto read binary le into buffer http://www.linuxquestions.org/questions/programming-9/...
0 Monday 03 September 2012 03:25 PM
-
7/31/2019 fread
7/10
thanx in advance...
your problem is more complicatedplease start your own thread
09-23-2009, 10:13 AM # 12
theNbomrSenior Member
Registered: Aug 2005Distribution: OpenSuse,Fedora, Redhat, DebianPosts: 4,667Blog Entries: 2
Rep:
Use the code posted by deiussum in article #8 of thisthread. That won't interpret the file in any way, butsimply display the bytes as hexadecimal. Interpretationof the data is application specific, and most people herewon't know how to extract meaningful information fromfiles of that type. For that, you will need some kind of published documentation, or a code library/API writtenfor the purpose of interpreting the file.--- rod.
09-23-2009, 12:44 PM # 13
smeezekittySenior Member
Registered: Sep 2009Location: Washington U.S.Distribution: Damn SmallLinux, KateOs, M$ IckdowsVista, My own OSPosts: 2,119
Rep:
did you see how many views on this thread?! LOL
01-14-2010, 06:40 AM # 14
naphstorLQ Newbie
Registered: Jan 2010Posts: 3
Rep:
reading a .m4v file.
hi all, i am a newbie in this forum. luckily i got the posti was searching for. but i have some issues. i have a.m4v file and want to read it and display its contents. iread the file and tried to print the buffer, but got some
junk data on screen. i even tried the code given abovefor printing buffer, but effort seems to be in vain. Theabove modified code i used is :-
Code:
#include
main(){
FILE *file;char *buffer;unsigned long fileLen;
wto read binary le into buffer http://www.linuxquestions.org/questions/programming-9/...
0 Monday 03 September 2012 03:25 PM
-
7/31/2019 fread
8/10
file = fopen("1.m4v", "rb");if (!file){
fprintf(stderr, "can't opeexit(1);
}
fseek(file, 0, SEEK_END);fileLen=ftell(file);fseek(file, 0, SEEK_SET);
buffer=(char *)malloc(fileLen+1);
if (!buffer){
fprintf(stderr, "Memory efclose(file);
exit(1);}
please help me out. thanks.
01-14-2010, 08:24 AM # 15
Sergei SteshenkoSenior Member
Registered: May 2005Posts: 4,168
Rep:
Quote:
Originally Posted by naphstorhi all, i am a newbie in this forum. luckily i got the post i was searching for. but i have someissues. 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
main(){
FILE *file;char *buffer;unsigned long fileLen;
file = fopen("1.m4v", "rb");if (!file){
fprintf(stderr, "caexit(1);
}
wto read binary le into buffer http://www.linuxquestions.org/questions/programming-9/...
0 Monday 03 September 2012 03:25 PM
-
7/31/2019 fread
9/10
fseek(file, 0, SEEK_END);fileLen=ftell(file);fseek(file, 0, SEEK_SET);
buffer=(char *)malloc(fileLe
if (!buffer){
fprintf(stderr, "Me fclose(file);exit(1);
}
please help me out. thanks.
What kind of data do you expect on the screen and whydo you expect it to look not like junk ? I.e. do you know
what data is displayed on screen as somethingmeaningful and what data as junk/gibberish ? If I may,have you ever heard of ASCII ? If not, try to performweb search on ASCII and/or try
man ascii
on your UNIX/Linux bix.
Page 1 of 2 1 2 >
Posting Rules
You may not post new threadsYou may not post repliesYou may not post attachmentsYou may not edit your posts
wto read binary le into buffer http://www.linuxquestions.org/questions/programming-9/...
0 Monday 03 September 2012 03:25 PM
-
7/31/2019 fread
10/10
Posting Rules
BB code is OnSmilies are On[IMG] code is Off HTML code is Off Trackbacks are Off Pingbacks are OnRefbacks are Off
Forum Rules
Similar Threads
Thread Thread Starter Forum Replies Last Post
read & write binary file error inredhat
xcmore Programming 1106-17-2005
07:48 AM
convert text file to binary excelfile
ust Linux - General 211-23-2004
02:33 AM
tcp/ip read and write buffer da_kidd_er Linux - Software 011-21-2004
04:13 PM
NULL buffer in read sys call
unpredictable jwstric2 Programming 3
09-02-2004
07:13 PMhowto disable/correct framebuffer? Distorts boot screen...
BroX Debian 308-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
wto read binary le into buffer http://www.linuxquestions.org/questions/programming-9/...