UNIX call system - copy file in C

I am trying to create a copy of the source file, but the target file is always empty.

Algorithm: reads from STDIN and writes to a source file, then reads from that file and writes text to a target file.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

#define BUFFSIZE 8192

int main(){
    int fdsource, fdtarget;
    int n, nr;
    char buff[BUFFSIZE];

    fdsource = open("source.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); // Create and open a source file in read/write
    if (fdsource < 0){
        printf("Source file open error!\n");
        exit(1);
    }

    fdtarget = open("target.txt", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); // Create and open a source file in write only
    if (fdtarget < 0){
        printf("Target file open error!\n");
        exit(1);
    }

    printf("\nInsert text:\n");
    while ((n = read(STDIN_FILENO, buff, BUFFSIZE)) > 0){ // Read from STDIN and write to source file
        if ((write(fdsource, buff, n)) != n){
            printf("Source file write error!\n");
            exit(1);
        }
    }

    while ((read(fdsource, buff, n)) > 0){ // Read from source file and write to target file
        if ((write(fdtarget, buff, n)) != n){
            printf("Source file open error!\n");
            exit(1);
        }
    }
    close(fdsource);
    close(fdtarget);
    exit(0);
    return 0;
}

      

+3


source to share


1 answer


The problem with your code is: "You opened both files at the start." To fix the problem, just open the original file in write mode and write all data, then close and reopen the original file in read mode. Then open the target file in write mode. The modified code is below and it has not been tested

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

#define BUFFSIZE 8192

int main(){
    int fdsource, fdtarget;
    int n;
    char buff[BUFFSIZE];

    fdsource = open("source.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); // Create and open a source file in read/write
    if (fdsource < 0){
        printf("Source file open error!\n");
        exit(1);
    }
    printf("\nInsert text:\n");
    while ((n = read(STDIN_FILENO, buff, BUFFSIZE)) > 0){ // Read from STDIN and write to source file
        if ((write(fdsource, buff, n)) != n){
            printf("Source file write error!\n");
            exit(1);
        }
    }

    close(fdsource);
    fdsource = open("source.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); // Create and open a source file in read/write
    if (fdsource < 0){
        printf("Source file open error!\n");
        exit(1);
    }

    fdtarget = open("target.txt", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); // Create and open a source file in write only
    if (fdtarget < 0){
        printf("Target file open error!\n");
        exit(1);
    }

    while ((read(fdsource, buff, n)) > 0){ // Read from source file and write to target file
        if ((write(fdtarget, buff, n)) != n){
            printf("Source file open error!\n");
            exit(1);
        }
    }
    close(fdsource);
    close(fdtarget);
    exit(0);
    return 0;
}

      



If you are wrong, use the above logic.

+2


source







All Articles