Home > OS >  How to read all the contents of a file using read() system call in C
How to read all the contents of a file using read() system call in C

Time:09-08

I want to read a file using read() system call and copy all its contents to another file. As the input file can be large, I don't know what buffer size to use. How to change the buffer size dynamically? Or is there any other approach like reading a file part by part using a fixed buffer ? Can anyone tell how to do this.

CodePudding user response:

Read the file part-by-part using a fixed buffer. To copy a file, there is no reason why you have to read the entire file in one call.

CodePudding user response:

If I understood your question correctly, each time you want to read with a different BUFFER_SIZE, read all the file and copy it to an other one. I think you can easily read the file using BUFFER_SIZE each time, and join the string to what you've read before until Read returns zero which means end of file(check [read(2) — Linux manual page][1]), than you can write the whole thing to the other file, I hope that this answered your question.

  • Related