0

Here is a C Project. I am working with text and binary files. This is a project that reads from stdin a file and also reads a new file and writes the information that the existed file has to the new file. But i get an error .This is my code:

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

    void main()
    {
        FILE *fFrom;
        FILE *fTo;
        char filename[80];
        char newfilename[80];

        printf("Enter the file you want to copy: ");
        scanf("%s", filename);

        fFrom = fopen(filename, "r");
        if(fFrom == NULL)
        {
            printf("File not found...");
            return;
        }

        printf("Enter new filename: ");
        scanf("%s", newfilename);

        fTo = fopen(newfilename, "r");
        if(fTo)
        {
            printf("File already exists...\n");
            return;
        }
        fclose(fTo);

        fTo = fopen(newfilename, "w");
        if(!fTo)
        {
            printf("Error opening file...\n");
            return;
        }

        while (!feof(fFrom))
        {
            fputc(fgetc(fFrom), fTo);
        }

        fclose(fFrom);
        fclose(fTo);

        return;
   }

Then In the console: Enter the file you want to copy: test.txt // existing file Enter new filename: new_test.txt // new file Segmentation fault (core dumped)

  • 2
    The handling of `fTo` seem to be illogical. You check if it can be open, and if it can - you return without closing. But if it cannot - you attempt close it. That would surely fail. – Eugene Sh. May 15 '20 at 19:25
  • Welcome to Stack Overflow. See ["Why is `while(!feof(file))` always wrong?"](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). Use `while(fgetc(file)!=EOF)` instead. – Beta May 15 '20 at 19:26
  • @EugeneSh. fTo is the new file that the user is going to create. In my code i check if the file already exists i exit the program and close the fTo that i opened – Andreas Soteriou May 15 '20 at 19:29
  • `fTo` is always `NULL` when you `fclose` it. – Eugene Sh. May 15 '20 at 19:35
  • @Beta i don't think that's the problem . I changed it the way you said and it shows the same error. – Andreas Soteriou May 15 '20 at 19:37
  • @EugeneSh omg that really worked, i removed the fclose(fTo) and it worked.. Why though i don't get it.. Thanks anyway!! – Andreas Soteriou May 15 '20 at 19:39
  • Why don't you get it? It is simple. If `fTo` is not `NULL`, you return. Otherwise (when it *is* `NULL`) you continue and call `fclose` on it. Ideally you should move the `fclose` into `if` block before return, so you close the file opened. – Eugene Sh. May 15 '20 at 19:41
  • @EugeneSh Ok i get it now. thanks. – Andreas Soteriou May 15 '20 at 19:44

1 Answers1

0

The seg fault is likely coming from the first call to fclose(fTo) when fTo is null (file doesn't exist).

See this answer for more discussion on calling fclose with a null FILE* pointer.

brady
  • 2,227
  • 16
  • 18
  • Whats your point though. I don't see the problem there. I check if the file already exists with the if(fTo) and if it exists i close fTo. If i remove the first fclose(fTo) it will not run – Andreas Soteriou May 15 '20 at 19:33