-2

I have this C Code below , where I want to copy information from one file (Including new lines ) to another one but nothing works for me.

Fa=fopen("a.txt","r");
while(!feof(Fa)){
    fscanf(Fa,"%[^\n]\n",b);
    printf("%s\n",b);
}
  • Does this answer your question? [check if file is empty or not in c](https://stackoverflow.com/questions/30133210/check-if-file-is-empty-or-not-in-c) – Kundan Nov 08 '20 at 07:39
  • You want `fgets()` if lines are important. – Shawn Nov 08 '20 at 07:40
  • 1
    Please see [Why is `while ( !feof (file) )` always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) Use the read operation to control the loop: `while(fscanf(Fa," %[^\n]", b) == 1)` or `while(fgets(b, sizeof b, Fa) != NULL)` (assuming `b` is an array, not a pointer). – Weather Vane Nov 08 '20 at 08:09
  • @Shawn But I think with fgets you can just read a specified number of characters, but I need to get separate sentences that I don't know their lengths. – Aymen Zidane Nov 11 '20 at 15:42
  • @WeatherVane but I don't know what does it mean , can you guide me for a book or video to understand it? – Aymen Zidane Nov 11 '20 at 15:43
  • I can guide you to the man page for [feof](https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/feof). It does not mean you have reached the end of file. It tells you that you have *attempted* to read a file that is *already* at the end. It is commonly misunderstood by learners, and I suppose from the frequency it appears in code posted here, by authors of some text books too. Used your way, you'll go one loop too many. – Weather Vane Nov 11 '20 at 15:52

1 Answers1

0

Program to copy one file to another

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


int main()
{
    FILE *sourceFile;
    FILE *destFile;
    char sourcePath[100];
    char destPath[100];

    char ch;

    /* Input path of files to copy */
    printf("Enter source file path: ");
    scanf("%s", sourcePath);
    printf("Enter destination file path: ");
    scanf("%s", destPath);

    /* 
     * Open source file in 'r' and 
     * destination file in 'w' mode 
     */
    sourceFile  = fopen(sourcePath, "r");
    destFile    = fopen(destPath,   "w");

    /* fopen() return NULL if unable to open file in given mode. */
    if (sourceFile == NULL || destFile == NULL)
    {
        /* Unable to open file hence exit */
        printf("\nUnable to open file.\n");
        printf("Please check if file exists and you have read/write privilege.\n");

        exit(EXIT_FAILURE);
    }


    /*
     * Copy file contents character by character.
     */
    ch = fgetc(sourceFile);
    while (ch != EOF)
    {
        /* Write to destination file */
        fputc(ch, destFile);

        /* Read next character from source file */
        ch = fgetc(sourceFile);
    }


    printf("\nFiles copied successfully.\n");


    /* Finally close files to release resources */
    fclose(sourceFile);
    fclose(destFile);

    return 0;
}
Mohammed Ali
  • 140
  • 2
  • 9