reading a file using fread()

S

syntax

hi, i want to read a file using fread() in equal chunks through a for
loop so that at the last call i dont get error..which way, i should
read it?

let me give an example, suppose i have 100 chars, i can read it in 50
chars for twice or 10 chars in ten times or 25 chars for 4
times...like this. Here, file size, i.e 100 chars is known to
me....but if the file size is unknown then how can i read in equal
chunks so that at the last call i dont face any problem?
 
L

Leor Zolman

hi, i want to read a file using fread() in equal chunks through a for
loop so that at the last call i dont get error..which way, i should
read it?

let me give an example, suppose i have 100 chars, i can read it in 50
chars for twice or 10 chars in ten times or 25 chars for 4
times...like this. Here, file size, i.e 100 chars is known to
me....but if the file size is unknown then how can i read in equal
chunks so that at the last call i dont face any problem?

It seems that you wouldn't face any problem even if you don't read your
data in "equal chunks". fread() returns the number of items successfully
read; just look at that value to find out how much you actually got.

Depending on the nature of your data, there's probably one "natural" way to
read that data in. If it is an ASCII file you're reading, you'd be better
off using text-based library functions such as fgets.

If it is binary data you're reading, you probably know something about the
structure of that data (so that the natural chunk size will be something
like the sizeof a struct used to contain the data).

No matter what, just check the return values of your input function calls
and you should be all set.
-leor

Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
B

Ben Pfaff

hi, i want to read a file using fread() in equal chunks through a for
loop so that at the last call i dont get error..which way, i should
read it?

let me give an example, suppose i have 100 chars, i can read it in 50
chars for twice or 10 chars in ten times or 25 chars for 4
times...like this. Here, file size, i.e 100 chars is known to
me....but if the file size is unknown then how can i read in equal
chunks so that at the last call i dont face any problem?

If the file size is unknown, you can't do that. Are you actually
asking how to find out the size of a file? If so, that's
addressed in the C FAQ. But keep in mind that, in general,
there's nothing keeping the size of a file from changing
(increasing or decreasing) while you read it.
 
R

Richard Bos

let me give an example, suppose i have 100 chars, i can read it in 50
chars for twice or 10 chars in ten times or 25 chars for 4
times...like this. Here, file size, i.e 100 chars is known to
me....but if the file size is unknown then how can i read in equal
chunks so that at the last call i dont face any problem?

Nice conundrum: in what size chunks do you divide a file which is
104917093 bytes large?

IYAM, a better strategy would be to use a natural-sized chunk and be
prepared to handle the final smaller bit. More solid, and,
off-topically, more likely to be efficient.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top