0

I was trying to copy what is written in a file to a different file (with system calls) but my code seems not to work. I have first tried just printing with printf() the buffer but it also does not work. My guess is that I'm reading the file incorrectly.

#define BUF_SIZE 200
int main(int argc, char *argv[]){

    int entrada,salida,leidos;
    char buffer[BUF_SIZE];

    entrada = open(argv[1],O_RDONLY);
    salida = creat(argv[2], 0644);

    while( (leidos = read(entrada,buffer,BUF_SIZE)) > 0 ){
        write(salida,buffer,leidos);

    }

    close(salida);
    close(entrada);

    return 0;
}

What's wrong with my implementation?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • 3
    you should probably start with looking at return values of functions; open/creat/read/write/close may fail silently in your code – OznOg Mar 11 '18 at 14:11
  • 1
    ... especially since in C++, the equivalent file copy is a single statement, doesn't need to use any buffer or a while loop. – Sam Varshavchik Mar 11 '18 at 14:11
  • @Biffen: Because it's a well-formed (albeit buggy) C++ program. OP is telling us that he intends to write this as a C++ program. – einpoklum Mar 11 '18 at 14:21

2 Answers2

1

I think you're missing the appropriate open flags on the output . Try:

salida = creat(argv[2], O_WRONLY | O_CREAT, 0644);

However, as comments suggest, you're probably getting errors indicated by the return values and/or the errno variable, which you are ignoring.

Also, I would avoid Castellano-specific variable names. Writing C/C++ requires knowing English anyway, so better stick to that for naming; otherwise - people who don't speak Castellano will have trouble understanding your code.

Finally - why are you doing it this way? There are much nicer C++-friendly, or even C-friendly, ways to copy a file - which would also be portable (your code isn't). See:

Copy a file in a sane, safe and efficient way

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Thank you very much for your answer! Sorry for using Castellano-specific variables I always try not to. As for your question; I did it this way because I am currently studying a computer engineering degree (the subject that Im writing the code for is: introduction to operating systems) and our teacher wanted us to use the systemcalls for reading, opening and writing files and not the functions. – Nicolas Granados Mar 11 '18 at 15:25
  • @NicolasGranados: If you find my answer useful, consider accepting and/or opvoting it (using the controls to the left of the message). – einpoklum Mar 11 '18 at 15:49
0

As @einpoklum stated, the main problem must be probably searched in the way you are opening your output file (flags and permissions). Overall, your code is far from implementing the minimum debugging verbosity and I think that a few code flow controls would help you a lot in detecting the real problem in your code:

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

#ifndef BUF_SIZE
#define BUF_SIZE 200
#endif

int main(int argc, char *argv[])
{
    int entrada = open(argv[1], O_RDONLY);

    if (entrada == -1)
    {
        fprintf(stderr, "Error opening: %s\n", argv[1]);
        exit(-1);
    }

    int openFlags = O_CREAT | O_WRONLY | O_TRUNC;
    mode_t filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    int salida = open(argv[2], openFlags, filePerms);

    if (salida == -1)
    {
        fprintf(stderr, "Error opening: %s\n", argv[2]);
        exit(-1);
    }

    ssize_t numRead;
    char buf[BUF_SIZE];

    while ((numRead = read(entrada, buf, BUF_SIZE)) > 0)
    {
        if (write(salida, buf, numRead) != numRead)
        {
            fprintf(stderr, "Writing error!\n");
            exit(-1);
        }
    }

    if (numRead == -1)
    {
        fprintf(stderr, "Reading error!\n");
        exit(-1);
    }

    if (close(entrada) == -1)
    {
        fprintf(stderr, "Input closing error!\n");
        exit(-1);
    }

    if (close(salida) == -1)
    {
        fprintf(stderr, "Output closing error!\n");
        exit(-1);
    }

    exit(0);
}

Many of the functions you use return values that can provide you an insight on that's going on. Use them.

Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98