在d盘根目录下的文件打不开 d:\fei.txt
#include<stdio.h>#include<string.h>
#include<stdlib.h>
void main()
{
FILE *fp;
char filename;
char s1,s2;
printf("please input the filename:");
gets(filename);
fp=fopen(filename,"w");
if(fp==NULL)
{
printf("cannot open file\n");
exit(0);
}
while((s1=getchar())!='\n')
{
fputc(s1,fp);
}
fclose(fp);
fp=fopen(filename,"r");
if(fp==NULL)
{
printf("cannot open file\n");
exit(0);
}
while((s2=fgetc(fp))!=EOF)
{
putchar(s2);
}
fclose(fp);
}
输入d:\fei.txt
显示cannot open file 本帖最后由 jackz007 于 2020-3-1 15:49 编辑
在我这里是完全正常的,下面是编译、运行实况:
C:\Bin>g++ -o x x.c
C:\Bin>x
please input the filename:abc.txt
hello,my friend,how are you?
hello,my friend,how are you?
C:\Bin>
由于你刻意要把文件放在一个根目录下,可能会存在权限问题,从而给程序运行带来不确定性,换一个非根目录试试呢。
页:
[1]