【发布时间】:2015-04-21 14:08:18
【问题描述】:
我正在尝试编写一个能够打开文本文件并将其拆分的程序,以便我可以将其保存在两个新文件中以更快地保存文件。但是使用我现在拥有的代码,我无法将我从原始文件中选择的字符打印到新文件中。
在我的文本文件中,我的文本是“荷兰人很高”。
在我想要获取的新文件中: 文件 1:Dthpol r tl 文件 2: uc epeaeal
这是我目前得到的代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char cUsb1;
char cUsb2;
char str[128];
FILE *ptr_readfile;
FILE *ptr_usb1;
FILE *ptr_usb2;
ptr_readfile = fopen("Dutch People.txt","r"); // open readfile
while(ptr_readfile != NULL) // keep running while readfile != null
{
if (ptr_readfile != EOF) // keep running while readfile != eof
{
cUsb1 = fgetc(ptr_readfile); // to get a char out of the readfile
ptr_usb1 = fopen("USB1.txt", "w"); // create and open USB1 file
fwrite(cUsb1 , str , str , ptr_usb1); //writing get c to file
cUsb2 = fgetc(ptr_readfile); // to get a char out of the readfile
ptr_usb2 = fopen("USB2.txt", "w"); // create and open USB2 file
fwrite(cUsb2 , str , str, ptr_usb2); //writing get c to file
fclose(ptr_usb1); // closing the file
fclose(ptr_usb2); // closing the file
}
break; // to stop the while loop
fclose(ptr_readfile); // closing the file
}
return 0;
}
【问题讨论】:
-
Dthpol r tl可能Dthpol r al,uc epeaeal-->uc epeaetl -
首先,我认为有必要阅读基本输入输出的教程。
-
你的程序完全错误。您应该: 1) 打开所有三个文件。 2)从“Dutch People.txt”中读取1个字符,写入“USB1.txt”,从“Dutch People.txt”中读取1个字符,写入“USB2.txt”。重复 2) 直到你读到一个 EOF。 3)关闭所有文件。提醒一下,
fgetc返回的是int而不是char。
标签: c file crash warnings fwrite