By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,203 Members | 1,633 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,203 IT Pros & Developers. It's quick & easy.

reading a file using fread()

P: n/a
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?
Nov 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 23 Feb 2004 11:55:25 -0800, sa*****@yahoo.com.hk (syntax) wrote:
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
le**@bdsoft.com
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
Nov 14 '05 #2

P: n/a
sa*****@yahoo.com.hk (syntax) writes:
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.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #3

P: n/a
sa*****@yahoo.com.hk (syntax) wrote:
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
Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.