File copy error in C language [closed]

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;
}
}
}
    
asked by anonymous 23.11.2014 / 15:37

1 answer

5

It's all a matter of properly indentifying 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 this while is in the wrong place. This return 0; should also not be within while . Fixing this works the way it should.

    
23.11.2014 / 15:47