2

I used the program below to read from a text file and write the same to a new file, but the new file always has some missing content at the end.

#include <stdio.h> #include <sys/stat.h> #include <unistd.h> #define CHUNKSIZE 256 int main(){ const char *file_name = "file.txt"; const char *new_file_name = "new_file.txt"; struct stat b_st; struct stat n_b_st; int file_size, new_file_size; char buffer[CHUNKSIZE]; FILE *fp = fopen(file_name, "rb"); FILE *fd = fopen(new_file_name, "wb"); while(fread(buffer, sizeof(buffer), 1, fp)){ fwrite(buffer, sizeof(buffer), 1, fd); fflush(fd); } stat(file_name, (struct stat *)&b_st); stat(new_file_name, (struct stat *)&n_b_st); file_size = b_st.st_size; new_file_size = n_b_st.st_size; if(file_size == new_file_size){ printf("Success reading and writing data"); exit(1); } return 0; } 

One point to notice is, as much i reduce the CHUNKSIZE, the amount of content missing at the end in new file is reduced and finally it gives success message when CHUNKSIZE is reduced to 1. How is it possible to read and write the complete file without changing CHUNKSIZE.

2
  • fread is not supposed to write into a file. I would consider changing the title. Commented Nov 30, 2014 at 12:56
  • Sorry for that silly mistake. Can you suggest me an appropriate answer ? Commented Nov 30, 2014 at 12:58

2 Answers 2

2
while(nread = fread(buffer, 1, CHUNKSIZE, fp)){ fwrite(buffer, 1, nread, fd); fflush(fd); } 

Write bytes which you read!

Read bytes are only returned when you set size 1.

On success, fread() and fwrite() return the number of items read or written. This number equals the number of bytes transferred only when size is 1. If an error occurs, or the end of the file is reached, the return value is a short item count (or zero).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot #parivana. That clears the whole confusion.
plus, when you set size more than 1, it returns 0 if it cannot read enough(size) bytes you set(usually meet eof). That is main reason you cannot get enough size new_file.txt.
0

the problem you have it is because your unit size is too big, try this:

int ret; while((ret = fread(buffer,1, sizeof(buffer), fp)) > 0){ fwrite(buffer, 1, ret, fd); fflush(fd);} 

read man pages for more information

you should also check return values for all of your program (for fopen and fread/fwrite).

6 Comments

Yeah, i described in my question about changing CHUNKSIZE to 1 and it worked as you suggested. But can't it be done without changing the buffer size to 1 because i have seen other programs with such values.
You will be still reading CHUNK_SIZE(256 bytes) buffer at each iteration, just fread will read CHUNK_SIZE units of 1
Does having size 1 or different makes difference in other aspects like speed etc. ?
I used your edit but this time it adds some additional content at the ending of file.
Edited my original response - try it now
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.