简体   繁体   中英

Copy content of a text file in another text file in C programming language

i have a problem on copying text of a file to another new file . It opens , creates a new file but there is nothing on it . It hasn't copied text of the first file. At the moment this is the code :

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

int main(void)
{
    char content[80];
    char newcontent[80];

    //Step 1: Open text files and check that they open//
    FILE *fp1, *fp2;

    fp1 = fopen("details.txt","r");
    fp2 = fopen("copydetails.txt","w");

    if(fp1 == NULL || fp2 == NULL)
    {
        printf("Error reading file\n");
        exit(0);
    }
    printf("Files open correctly\n");

    //Step 2: Get text from original file//
    while(fgets(content, sizeof(content), fp1) !=NULL)
    {
        fputs (content, stdout);
        strcpy (content, newcontent);
    }

    printf("%s", newcontent);
    printf("Text retrieved from original file\n");

    //Step 3: Copy text to new file//
    while(fgets(content, sizeof(content), fp1) !=NULL)
    {
        fprintf(fp2, newcontent);
    }
    printf("file created and text copied to it");

    //Step 4: Close both files and end program//
    fclose(fp1);
    fclose(fp2);

    getch();
    return 0;
}
while(fgets(content, sizeof(content), fp1) !=NULL){
    fprintf(fp2, "%s", content);
}

Have you try to just use the write() function?

Just pass the file descriptor of the new file give by open. man write for more information.

There are some errors in your code.

  1. strcpy (content, newcontent);

    If you want to store the file content in "newcontent" use strcat(). strcpy() will overwrite the destination on each call. In addition to that, the first argument should be the destination. Here you are trying to overwrite what you just read from the file. Use strcat(newcontent,content); Since we are going to concatenate the text to the initial value of "newcontent", the declaration is modified to char newcontent[80]="" ;

  2. fprintf(fp2, newcontent); Add the format specifier. Are you not getting any compilation error? And one more thing. You are reading the text from source file to "content" and writing the contents of "newcontent" to the destination file. Use fprintf(fp2,"%s",content);

  3. File Pointer Position : After the first while loop, file position indicator is at the end of the file. So body of the second while loop won't execute even once. Use rewind(fp1); or fseek(fp1,0L,SEEK_SET); to bring back the file position indicator to the beginning.

Please see the corrected code.

 #include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { char content[80]; char newcontent[80]=""; // Change 1: Initialize the array //Step 1: Open text files and check that they open// FILE *fp1, *fp2; fp1 = fopen("details.txt","r"); fp2 = fopen("copydetails.txt","w"); if(fp1 == NULL || fp2 == NULL) { printf("Error reading file\\n"); exit(0); } printf("Files open correctly\\n"); //Step 2: Get text from original file// while(fgets(content, sizeof(content), fp1) !=NULL) { fputs (content, stdout); strcat (newcontent, content); // Change 2: Append the new line to "newcontent" } printf("%s", newcontent); printf("\\nText retrieved from original file\\n"); fseek(fp1,0L,SEEK_SET); // Change 3: Bring file pointer to the beginning. May use rewind(fp1); also //Step 3: Copy text to new file// while(fgets(content, sizeof(content), fp1) !=NULL) { fprintf(fp2,"%s",content); } printf("file created and text copied to it"); //Step 4: Close both files and end program// fclose(fp1); fclose(fp2); getch(); return 0; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM