Home > Software design >  how to copy a file in c byte by byte
how to copy a file in c byte by byte

Time:10-06

okay so I am studying C and in a textbook there is an exercise left for the reader leading up to this exercise the book explains that a file (or at least a regular file) is simply an array of bytes. it is my understanding that in linux each block is 512 bytes but when you read a file into a file descriptor then the program automatically loads up the entire file potentially correct? the book goes over the Linux system calls read, write, and lseek (some basic low-level calls).

now the goal is like i said to copy a file byte by byte to some random new file. pseudo-code will be perfectly acceptable. In any case it is my understanding that you need

My understanding is that the build would go something like this:

#include <all of the req'd headers>

int main(int argc,char** argv) {
    char buff[1]; // byte by byte buffer 
    inf fd = open("filename", O_RDONLY);
    
    int fdp[2]; // for the pipe's fd's
    pipe(fdp);
    
    if( fork() == 0 ) {
        close(fdp[0]);
        
    }

    else {
        write("newfile", buff, 1);
    }

}

for me at least at this stage this is really quite a complex question. My thinking is to read into the file descriptor the entire contents of the file then using a pipe to write byte by byte into a new file copy the entire contents of the previous file into the new file. I am also unsure as to how lseek helps us here in this situation. Or am I over-complicating this and am in the completely wrong direction?

CodePudding user response:

As has been stated in the comments, you don't need a pipe for this at all. Honoring the 'one byte at a time' requirement, your code might look something like this:

FILE *in = fopen("sourceFile", "rb");
    
if (in)
{
    FILE *out = tmpfile();
    if (out)
    {
        int c;
        while ((c = fgetc(in)) != EOF)
            fputc(c, out);
        fclose(out);
    }
    fclose(in);
}

And you're done. Please remember,that tmpfile will get deleted once you close it, so you may want to do something with the contents prior to closing it. Or open a more permanent file.

CodePudding user response:

Okay so i have it figured out the solution is actually very simple! Only about 10 lines of code or so here it is:

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>



int main(int argc, char ** argv) { 
    
    char oneByte;
    
    int n;
    int source;
    int destination;
    
    source = open("notes.txt", O_RDONLY);
    destination = open("copiedFile.txt", O_RDWR | O_CREAT, 0644);
    
    while ((n = read(source, &oneByte, 1)) > 0) {
        write(destination, &oneByte, 1);
    }
    
    close(source);
    close(destination);
    
    return 0;
}
  • Related