【发布时间】:2011-10-18 07:56:37
【问题描述】:
这是我的教授用他自己的话告诉我的。
“编写一个程序,复制用户指定的文本文件的内容并将其复制到另一个文本文件“copy.txt”。文件中的任何行都不应超过 256 个字符。”
这是我到目前为止用他的信息设计的代码:
#include <stdio.h>
#include <io.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
int main ( void )
{
char filename[256]=""; //Storing File Path/Name of Image to Display
static const char file2name[] = "C:\\Users\\Rael Jason\\Desktop\\Rael's iPod\\Codeblocks\\copy.txt";
FILE *file;
FILE *write;
printf("Please enter the full path of the text file you want to copy text: ");
scanf("%s",&filename);
file = fopen ( filename, "r" );
file = fopen (file2name, "r" );
if ( file != NULL )
{
char line [ 256 ]; /* or other suitable maximum line size */
char linec [256]; // copy of line
while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
{
fputs ( line, stdout ); /* write the line */
strcpy(linec, line);
fprintf (write , linec);
fprintf (write , "\n");
}
fclose (write);
fclose ( file );
}
else
{
perror ( filename ); /* why didn't the file open? */
}
return 0;
}
我似乎无法正确完成文件写入?你能帮忙吗?
【问题讨论】:
-
不应该把 file = fopen (file2name, "r" ) 写成 = fopen(file2name, "w") 吗?
-
再次检查打开文件的行。我相信你会想出来的! :)