1

This is my code

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

int main(int argc, char *argv[]) {

    if (argc < 4) {
        printf("Missing arguments\n");
        return -1;
    }

    // Check if buffer is valid before reading anything
    int bufferSize = atoi(argv[3]);
    if (!bufferSize || bufferSize < 1) {
        printf("Invalid buffer size\n");
        return -1;
    }

    printf("*** Copying from '%s' to '%s' (Buffer size: %dB) ***\n",
           argv[1], argv[2], bufferSize);

    // READ SOURCE FILE
    FILE *inputFile = fopen(argv[1], "r");
    if (!inputFile) {
        printf("Error opening source file\n");
        return -1;
    }

    // READ DESTINATION FILE
    FILE *outputFile = fopen(argv[2], "w");
    if (!outputFile) {
        printf("Error opening destination file\n");
        return -1;
    }

    int buffer[bufferSize];
    int bytes;
    do {
        bytes = fread(buffer, 1, bufferSize, inputFile);
        if (fwrite(buffer, 1, bytes, outputFile) != bytes) {
            printf("Error writing into destination file\n");
            return -1;
        }
    } while (bytes > 0);

    fclose(inputFile);
    fclose(outputFile);

    return 0;
}

But when I try to exe the file it doesn't work. What could be the problem? Here's the command line:

/Users/jurajc/Documents/Program/C/L1\ 1/C_program/c_program file.txt fileCopy.txt 512 
*** Copying from 'file.txt' to 'fileCopy.txt' (Buffer size: 512B) ***
Error opening source file
chqrlie
  • 131,814
  • 10
  • 121
  • 189
JurajC
  • 99
  • 2
  • 8
  • Where is located input file compared to the dir from where you start program ? Do ´ls -l’ – Ptit Xav Nov 06 '21 at 14:52
  • You can find how to print error info in this [post](https://stackoverflow.com/questions/1746510/unable-to-open-a-file-with-fopen) – Ptit Xav Nov 06 '21 at 14:56

1 Answers1

0

The input file file.txt cannot be opened: either because it is not present in the current directory or because you do not have read access to it.

You should output more informative error messages. Note also these problems:

  • if (!bufferSize || bufferSize < 1) is a redundant test. if (bufferSize < 1) is sufficient.
  • the error messages should be output to stderr
  • the files should be open in binary mode to reliably copy all file types on legacy systems.
  • the read/write loop is incorrect: you should stop when fread returns 0 before attempting to write 0 elements to the output file.

Here is a modified version:

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {

    if (argc < 4) {
        fprintf(stderr, "Missing arguments\n");
        return -1;
    }

    // Check if buffer is valid before reading anything
    int bufferSize = atoi(argv[3]);
    if (bufferSize < 1) {
        fprintf(stderr, "Invalid buffer size: %s\n", argv[3]);
        return -1;
    }

    printf("*** Copying from '%s' to '%s' (Buffer size: %dB) ***\n",
           argv[1], argv[2], bufferSize);

    // READ SOURCE FILE
    FILE *inputFile = fopen(argv[1], "rb");
    if (!inputFile) {
        fprintf(stderr, "Error opening source file %s: %s\n", 
                argv[1], strerror(errno));
        return -1;
    }

    // READ DESTINATION FILE
    FILE *outputFile = fopen(argv[2], "wb");
    if (!outputFile) {
        fprintf(stderr, "Error opening destination file %s: %s\n", 
                argv[2], strerror(errno));
        return -1;
    }

    int buffer[bufferSize];
    int bytes;
    while ((bytes = fread(buffer, 1, bufferSize, inputFile)) != 0) {
        if (fwrite(buffer, 1, bytes, outputFile) != bytes) {
            fprintf(stderr, "Error writing into destination file: %s\n", strerror(errno));
            return -1;
        }
    }

    fclose(inputFile);
    fclose(outputFile);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189