Exactly one byte from binary file.

Networking/Security Forums -> Programming and More

Author: thaw^ PostPosted: Mon May 23, 2005 4:03 pm    Post subject: Exactly one byte from binary file.
    ----
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.

Author: zeedoLocation: Scotland PostPosted: Mon May 23, 2005 4:09 pm    Post subject:
    ----
http://perlhelp.web.cern.ch/PerlHelp/lib/Pod/perlfunc.html#item_read

Author: thaw^ PostPosted: Mon May 23, 2005 4:17 pm    Post subject:
    ----
Okay, that's for Perl. What about C?

Author: zeedoLocation: Scotland PostPosted: Mon May 23, 2005 4:23 pm    Post subject:
    ----
for C just use the functions you mentioned.

fread will do fine

Author: UziMonkey PostPosted: Mon May 23, 2005 8:04 pm    Post subject:
    ----
thaw^ wrote:
Okay, that's for Perl. What about C?


Look at the fopen, fseek and fread functions.

Author: thaw^ PostPosted: Tue May 24, 2005 10:59 pm    Post subject:
    ----
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.

Author: capiLocation: Portugal PostPosted: Wed May 25, 2005 12:13 am    Post subject:
    ----
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).

Author: mmelton PostPosted: Wed May 25, 2005 2:47 pm    Post subject:
    ----
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

Author: UziMonkey PostPosted: Wed May 25, 2005 8:59 pm    Post subject:
    ----
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



Networking/Security Forums -> Programming and More


output generated using printer-friendly topic mod, All times are GMT + 2 Hours

Page 1 of 1

Powered by phpBB 2.0.x © 2001 phpBB Group