这是一个简单的程序,应该复制一个内容
文件名为copyme
的文件here
。我通过以下命令创建了copyme
,其中包含一些文本:
touch copyme.txt
open copyme.txt
然后我键入文本,并保存文件
touch copyme.txt
命令。
然后我编写了一个程序:
// Program to copy one file ot another
#include <stdio.h>
int main (void)
{
char in_name[64], out_name[64];
FILE *in, *out;
int c;
// get file names from user
printf("Enter name of file to be copied: ");
scanf("%63s", in_name);
printf("Entere name of output file: ");
scanf("%63s", out_name);
// open input and output files
if ( (in = fopen(in_name, "r")) == NULL)
{
printf("Can't open %s for reading.\n", in_name);
return 1;
}
if ( (out = fopen(out_name, "w")) == NULL)
{
printf("Can't open %s for writing.\n", out_name);
return 2;
}
while ((c = getc(in)) != EOF)
putc(c, out);
// Close open files
fclose (in);
fclose (out);
printf("File has been copied\n");
return 0;
}
然后在终端跑了。 这是输出:
Enter name of file to be copied: copyme
Entere name of output file: here
Can't open copyme for reading.
编译器不识别copyme
文件,尽管它是
物理存在于文件夹中(我看到了,我打开它,我读了
它)。
我很感激你的帮助。我是这个新手。
谢谢!
答案 0 :(得分:2)
变化
if ( (in = fopen(in_name, "r")) == NULL)
{
printf("Can't open %s for reading.\n", in_name);
return 1;
}
到
#include <errno.h>
if ( (in = fopen(in_name, "r")) == NULL)
{
perror("Can't open file for reading.\n");
return 1;
}
您将收到一条人类可读的消息,告诉您为何无法读取该文件