0
這是我的教授用他自己的話提供給我的。複製文本文件的內容並將其複製到另一個
「編寫一個程序,複製用戶指定的文本文件的內容並將其複製到另一個文本文件」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」)be write = fopen(file2name,「w」)? – DeCaf
檢查再次打開文件的行。我相信你會弄明白的! :) –