将数据从一个文本文件保存并传输到另一个文本文件

时间:2022-09-12 19:31:45

Having problems with transferring a data from one text file to another. My idea is to transfer it per character but it seems to not be working. I have added these codes at the beginning

将数据从一个文本文件传输到另一个文本文件时遇到问题。我的想法是每个角色转移它,但它似乎不起作用。我在开头添加了这些代码

FILE *addressPtr;
FILE *ressPtr;   

and

addressPtr = fopen("temporary.txt","w");

These set of codes are in a switch condition

这些代码集处于切换状态

fclose(addressPtr);
addressPtr = fopen("temporary.txt","r");
while((filechar = fgetc(addressPtr)) != EOF){
    fclose(addressPtr);
    ressPtr = fopen("Address Book.txt","a");
    fprintf(ressPtr,"%c",filechar);
    fclose(ressPtr);
    addressPtr = fopen("temporary.txt","r");
}
printf("The file has been successfully saved!!");

I just learned about these file operations and dont really know what went wrong

我刚刚了解了这些文件操作,并不知道出了什么问题

If I open my Address Book.txt file it will only display the first character of my temporary.txt but continuously (never-ending).

如果我打开我的Address Book.txt文件,它将只显示我的temporary.txt的第一个字符,但是连续(永无止境)。

1 个解决方案

#1


-1  

You need to open the files in binary mode.

您需要以二进制模式打开文件。

addressPtr = fopen("temporary.txt","wb");

also write in binary mode using "rb" to open the file.

也可以使用“rb”以二进制模式写入打开文件。

I'm not sure why you're using fopen inside a loop. But if you want to copy a file then this is some working code I've tested:

我不确定你为什么在循环中使用fopen。但是如果你想复制一个文件,那么这是我测试过的一些工作代码:

#include<stdio.h>

int main()
{
    FILE *addressPtr;
    FILE *ressPtr;
    char filechar = '\0';

    addressPtr = fopen("D:\\test\\source.jpg","rb");
    ressPtr = fopen("D:\\test\\dest.jpg","wb");

    while(!feof(addressPtr))
    {
        filechar = fgetc(addressPtr);
        fprintf(ressPtr,"%c",filechar);
    }
    fclose(addressPtr);
    fclose(ressPtr);
    printf("The file has been successfully saved!!");

}

This essentially copies a file called source.jpg to another file called dest.jpg.

这基本上将名为source.jpg的文件复制到另一个名为dest.jpg的文件中。

Hope you got your answer.

希望你得到你的答案。

#1


-1  

You need to open the files in binary mode.

您需要以二进制模式打开文件。

addressPtr = fopen("temporary.txt","wb");

also write in binary mode using "rb" to open the file.

也可以使用“rb”以二进制模式写入打开文件。

I'm not sure why you're using fopen inside a loop. But if you want to copy a file then this is some working code I've tested:

我不确定你为什么在循环中使用fopen。但是如果你想复制一个文件,那么这是我测试过的一些工作代码:

#include<stdio.h>

int main()
{
    FILE *addressPtr;
    FILE *ressPtr;
    char filechar = '\0';

    addressPtr = fopen("D:\\test\\source.jpg","rb");
    ressPtr = fopen("D:\\test\\dest.jpg","wb");

    while(!feof(addressPtr))
    {
        filechar = fgetc(addressPtr);
        fprintf(ressPtr,"%c",filechar);
    }
    fclose(addressPtr);
    fclose(ressPtr);
    printf("The file has been successfully saved!!");

}

This essentially copies a file called source.jpg to another file called dest.jpg.

这基本上将名为source.jpg的文件复制到另一个名为dest.jpg的文件中。

Hope you got your answer.

希望你得到你的答案。