LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 01-14-2010, 08:36 AM   #16
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46

Code:
dump_buffer(&buffer, fileLen);
 
Old 01-14-2010, 09:00 AM   #17
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by carbonfiber View Post
Code:
dump_buffer(&buffer, fileLen);
Yep, it's a mistake, but, still, in the very first post:


Quote:
Could somebody provide a complete example of code that shows how to read a binary file into a buffer/array in C
.

I.e. even a correct WRT the above mistake program won't print much meaningful data because of

Code:
printf("%c", ((char *)buffer)[i]);
.
 
Old 01-14-2010, 09:59 AM   #18
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
If all you want to do is see the content of a binary file in a 'readable' format, then od is a useful tool.
--- rod.
 
Old 01-14-2010, 10:44 AM   #19
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
Sergei Steshenko: I know. I wasn't disagreeing with your post, I was simply pointing out one mistake I thought would tourment him later on.
 
Old 01-14-2010, 10:57 PM   #20
naphstor
LQ Newbie
 
Registered: Jan 2010
Posts: 3

Rep: Reputation: 0
Quote:
Originally Posted by Sergei Steshenko View Post
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.
hi i m sorry, if had done any mistake. i actually meant to say that my output screen shows output in ascii data, then it holds for some seconds and prints something weird, which doesnt look like ascii. and then in the last it stops by saying segmentation fault.
so coz of that i said i am getting some junk data. the output i got is shown below.

Quote:
[root@is02vmbig code]# ./test4.out
value isð2ÀH,ü¿3n ð2ÀH,ü¿3nt,ü¿|,ü¿FKôo¨,ü¿H,ü¿ð+ü¿õmÔï
JÐSÔï
½
D t,ü¿ÐSl,ü¿¿0¿<¿Z¿e¿u¿¿ª¿½¿Ç¿þ¿¤þ¿*þ¿Âþ¿æþ¿÷þ¿*þ¿2þ¿=þ¿Jþ¿þ¿«þ¿Àþ¿Îþ¿ÿóëd
[-ü¿i686PuTTY./test4.outHOSTNAME=is02vmbig.ittiam.comTERM=xtermSHELL=/bin/bashHISTSIZE=1000SSH_CLIENT=::ffff:172.20.33.23 4410 22SSH_TTY=/dev/pts/1USER=rootLS_COLORS=no=00:fi=00:di=00;34:ln=00;36i=40;33:so=00;35:bd=40;33;01:cd=40;33;01r=01;05 ;37;41:mi=01;05;37;41:ex=00;32:*.cmd=00;32:*.exe=00;32:*.com=00;32:*.btm=00;32:*.bat=00;32:*.sh=00;3 2:*.csh=00;32:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z= 00;31:*.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31:*.cpio=00;31:*.jpg=00;35:*.gif=00;35: *.bmp=00;35:*.xbm=00;35:*.xpm=00;35:*.png=00;35:*.tif=00;35:MAIL=/var/spool/mail/rootPATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin:/root/mv_pro_4.0/montavista/pro/devkit/arm/v5t_le/bin:/root/mv_pro_4.0/montavista/pro/devkit/arm/v5t_le/armv5tl-montavista-linuxeabi/binINPUTRC=/etc/inputrcPWD=/win_d/WORKS/NEW_TOOL/TEST/codeLANG=en_US.UTF-8SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpassSHLVL=1HOME=/rootLOGNAME=rootSSH_CONNECTION=::ffff:172.20.33.23 4410 ::ffff:172.20.33.44 22LESSOPEN=|/usr/bin/lesspipe.sh %sG_BROKEN_FILENAMES=1_=./test4.outOLDPWD=/win_d/WORKS/NEW_TOOL/TEST./test4.outSegmentation fault
now i cant make out why is it showing this kind of output.
thanks.
 
Old 01-14-2010, 11:55 PM   #21
naphstor
LQ Newbie
 
Registered: Jan 2010
Posts: 3

Rep: Reputation: 0
my actual requirement is that, i have a bitstream of some 100mb. now i want to extract some 1000 bytes from it. well i dont know exactly how to perform this action so i tried to fread the file. but i still dont know how to extract some fixed number of data from binary file and display it in bits format.

would appreciate any reply.
 
Old 01-15-2010, 04:05 AM   #22
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by naphstor View Post
hi i m sorry, if had done any mistake. i actually meant to say that my output screen shows output in ascii data, then it holds for some seconds and prints something weird, which doesnt look like ascii. and then in the last it stops by saying segmentation fault.
so coz of that i said i am getting some junk data. the output i got is shown below.



now i cant make out why is it showing this kind of output.
thanks.
As I have already asked, why do you expect a different output ? I.e. justify your expectations first.
 
Old 01-15-2010, 04:06 AM   #23
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by naphstor View Post
my actual requirement is that, i have a bitstream of some 100mb. now i want to extract some 1000 bytes from it. well i dont know exactly how to perform this action so i tried to fread the file. but i still dont know how to extract some fixed number of data from binary file and display it in bits format.

would appreciate any reply.
Try

man split

first.
 
Old 11-27-2014, 05:02 PM   #24
Corsari
Member
 
Registered: Oct 2004
Posts: 57

Rep: Reputation: 15
Question

Do any of you have any clue of how to apply this to read RAW disk content?

I'm doing some learning stage of ANSI C under linux and in my first attempt

I'd like

- read raw sectors form e.g /dev/sdb
- sequentially get one sector (so its 512 bytes) at a time
- print on the terminal the 512 bytes as ASCII characters (not formatted, just a raw output, ok, also formatted but it is not a must)
- at next sector, overwrite the precedent 512 characters (terminal should not scroll up)
- loop so fast until a user break happen

Thank you for helps
Cor

Last edited by Corsari; 11-27-2014 at 05:04 PM.
 
Old 11-27-2014, 05:32 PM   #25
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
The majority of the question from the original poster (and you should have started your own thread, not hijack an existing and quite old one) has to do with the representation of his binary data. The problem is that ASCII doesn't define all 256 bytes as characters, and many of the one that it does define are not printable as human-readable glyphs.
Having said that, I suggest NOT reading partitions or block devices as a learning exercise. Read/write files, instead. When you print the contents, format it in human readable form, such as hex or octal text, with lines that break on friendly byte boundaries, etc. Example (untested)
Code:
for( i = 0; i < sizeof( myBuffer ); i++ ){
    if( i % 16 == 0 ){
        printf( "\n%08X: ", i );
    }
    printf( "%02X ", myBuffer[i] );
}
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration