Error copy file in C language

Asked

Viewed 83 times

0

Hi! I’m trying to create a copy of a C file, but I’m not able to copy the content, it’s just creating a new blank file, can anyone see where I’m going wrong?

#include <stdio.h>

int main() {
    char str[100];
    FILE *file = fopen("/home/ananda/Downloads/pratica12/GAAL.txt", "r");
    if(file == NULL){
        printf("arquivo inexistente\n");
        return 0;
    }

    FILE *file1 = fopen("/home/ananda/Downloads/pratica12/GAAL2.txt", "w");
    if(file == NULL){
        printf("arquivo inexistente\n");
        return 0;


   while (fgets (str, 100, file) != NULL){
       fputs(str, file1);
return 0;
}
}
}

1 answer

5

It is all a matter of correctly identar the code. Putting the spaces in the places due the error is immediately clear:

#include <stdio.h>

int main() {
    char str[100];
    FILE *file = fopen("/home/ananda/Downloads/pratica12/GAAL.txt", "r");
    if(file == NULL){
        printf("arquivo inexistente\n");
        return 0;
    }

    FILE *file1 = fopen("/home/ananda/Downloads/pratica12/GAAL2.txt", "w");
    if(file == NULL){
        printf("arquivo inexistente\n");
        return 0;


        while (fgets (str, 100, file) != NULL){
            fputs(str, file1);
            return 0;
        }
    }
}

I imagine that while is in the wrong place. This return 0; should not be inside the while. Fixing it the code works as it should.

  • I think it’s amazing how this still needs to be the most given advice.

  • 3

    A lot of people who are starting to think that ident is freshness. Is not.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.