C program To read 1000 bytes at a time from a File

R

ramesh

can any one help me to solve this one we need to write a c program,


Consider a file of size 5000 bytes. Open the file and read to a buffer
of size buf[1000];

We have to read 1000 bytes only from the file at a time and after
displaying the file we have to lseek 500 bytes back in the file

And read file from 500 to 1000. we have to read this type upto end of
file .


Rgds

Ramesh
 
T

thampan

you have written the algorithm exactly as you want it .. what is
preventing you from coding it yourself ?
 
I

Ian Collins

ramesh said:
can any one help me to solve this one we need to write a c program,


Consider a file of size 5000 bytes. Open the file and read to a buffer
of size buf[1000];

We have to read 1000 bytes only from the file at a time and after
displaying the file we have to lseek 500 bytes back in the file

And read file from 500 to 1000. we have to read this type upto end of
file .
What have you tried?

You should be able to do this with standard IO functions (fopen, fread,
fseek).
 
R

Rod Pemberton

ramesh said:
can any one help me to solve this one we need to write a c program,


Consider a file of size 5000 bytes. Open the file and read to a buffer
of size buf[1000];

We have to read 1000 bytes only from the file at a time and after
displaying the file we have to lseek 500 bytes back in the file

And read file from 500 to 1000. we have to read this type upto end of
file .

suggestion: read only 500 bytes at a time. It will eliminate an lseek.

?type? buf[1000];
?type? buf2[500];

fread() 500 using into buf2[] /* buf2 has 0-500 */
memcpy() buf2[] into buf[0] /* buf has 0-500 */
fread() 500 using into buf2[] /* buf2 has 500-100 */
memcpy() buf2[] into buf[500] /* buf has 0-500 and 500-1000 */
display 1000 bytes of buf[];
process 500 bytes of buf2[]; /* buf2 still has bytes 500-1000 */
loop & process 500 bytes of buf2[]; /* bytes 1000+, 500 at a time */


Rod Pemberton
 

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
474,183
Messages
2,570,966
Members
47,514
Latest member
AdeleGelle

Latest Threads

Top