• RSS
  • Twitter
  • FaceBook

Security Forums

Log in

FAQ | Search | Usergroups | Profile | Register | RSS | Posting Guidelines | Recent Posts

Exactly one byte from binary file.

Users browsing this topic:0 Security Fans, 0 Stealth Security Fans
Registered Security Fans: None
Post new topic   Reply to topic   Printer-friendly version    Networking/Security Forums Index -> Programming and More

View previous topic :: View next topic  
Author Message
thaw^
Just Arrived
Just Arrived


Joined: 24 Apr 2005
Posts: 0


Offline

PostPosted: Mon May 23, 2005 4:03 pm    Post subject: Exactly one byte from binary file. Reply with quote

Hello all!

How do I read one byte froma binary file? And then (later) another one? And again and again, blah-blah.

Should I use fread/fwrite, fgetc or is that something else? (i'm using C, but I'd be glad to now how to do it in Perl)

Duh, I'm confused.
Back to top
View user's profile Send private message
zeedo
SF Reviewer
SF Reviewer


Joined: 01 Sep 2004
Posts: 24
Location: Scotland

Offline

PostPosted: Mon May 23, 2005 4:09 pm    Post subject: Reply with quote

http://perlhelp.web.cern.ch/PerlHelp/lib/Pod/perlfunc.html#item_read
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
thaw^
Just Arrived
Just Arrived


Joined: 24 Apr 2005
Posts: 0


Offline

PostPosted: Mon May 23, 2005 4:17 pm    Post subject: Reply with quote

Okay, that's for Perl. What about C?
Back to top
View user's profile Send private message
zeedo
SF Reviewer
SF Reviewer


Joined: 01 Sep 2004
Posts: 24
Location: Scotland

Offline

PostPosted: Mon May 23, 2005 4:23 pm    Post subject: Reply with quote

for C just use the functions you mentioned.

fread will do fine
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
UziMonkey
SF Reviewer
SF Reviewer


Joined: 19 Dec 2003
Posts: 5


Offline

PostPosted: Mon May 23, 2005 8:04 pm    Post subject: Reply with quote

thaw^ wrote:
Okay, that's for Perl. What about C?


Look at the fopen, fseek and fread functions.
Back to top
View user's profile Send private message Visit poster's website
thaw^
Just Arrived
Just Arrived


Joined: 24 Apr 2005
Posts: 0


Offline

PostPosted: Tue May 24, 2005 10:59 pm    Post subject: Reply with quote

I'm guessing that using read() and write() will do just as fine?

Yes, I know - that's for low-level IO, but assume I use open instead of fopen, it should work, yes?

Sorry if I'm irritating You with my newbish questions, but to be honest C can be confusing. Especially when there are so many functions to use, while each one does exactly the same.
Back to top
View user's profile Send private message
capi
SF Senior Mod
SF Senior Mod


Joined: 21 Sep 2003
Posts: 16777097
Location: Portugal

Offline

PostPosted: Wed May 25, 2005 12:13 am    Post subject: Reply with quote

Using open, read, write and close will be just fine, as long as you don't need your program to be executed in non-POSIX systems (e.g. non-*nix stuff, PCDOS-like OSes such as DOS or Windoze etc). These functions are system calls, they are functions of the operating system itself, rather than the C library.

Regarding C, I would hardly say there "each function does exactly the same"... Each function has its own purpose and objective. fread and fwrite are meant for buffered binary input/output. They are the general case, what you can use to read or write N bytes on some stream. fgetc, getc, fputc, putc, etc are specifically for one byte. As they are more specific, they will usually be better optimized for that particular case. fputs and friends are for writing a string on a stream. As for fprintf and company, they are for formatted output.

Can you use one function to do the job of another? Sure, in some cases. You can just use fread to do pretty much all your input, with some added work here or there. You can use fputc to write 5000 characters, if you do it in a loop. Just because you can put a nail in place with a brick doesn't mean that a brick does the same thing as a hammer, or that the two are mutually redundant.

These I/O C functions are meant as an abstraction over the underlying operating system syscalls. fread will eventually call whatever system call the OS has for reading (read, for example, in *nix systems) to do the actual reading, as will every other I/O function that needs to read from a file descriptor. The point of having these functions is portability (if you use the C library functions you don't have to worry about different OSes such as Windows where the syscalls are different), and added functionality. Having specific tools to do specific jobs can make your life easier if, say, you want to print out a formatted string (easier to just call fprintf from the C library than it is to implement it yourself by using nothing more than the write syscall).
Back to top
View user's profile Send private message
mmelton
Just Arrived
Just Arrived


Joined: 19 May 2005
Posts: 0


Offline

PostPosted: Wed May 25, 2005 2:47 pm    Post subject: Reply with quote

I hate to point this out, but try to _avoid_ reading one "byte" from a file if your trying to read characters or words aligned to 8bits. With unicode support ever increasing, it can be a dangerous game! Even if you're simply reading a number, bare in mind that there could be a pair of nulls every other byte.

Matt
Back to top
View user's profile Send private message
UziMonkey
SF Reviewer
SF Reviewer


Joined: 19 Dec 2003
Posts: 5


Offline

PostPosted: Wed May 25, 2005 8:59 pm    Post subject: Reply with quote

mmelton wrote:
I hate to point this out, but try to _avoid_ reading one "byte" from a file if your trying to read characters or words aligned to 8bits. With unicode support ever increasing, it can be a dangerous game!

Well, he did say it was a binary file..

Quote:
Even if you're simply reading a number, bare in mind that there could be a pair of nulls every other byte.

?? UTF8 or ASCII text won't have this. And he did say it was a binary file Wink

Moderator note: edited to fix quote tags - capi
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   

Post new topic   Reply to topic   Printer-friendly version    Networking/Security Forums Index -> Programming and More All times are GMT + 2 Hours
Page 1 of 1


 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Community Area

Log in | Register