一个关于文件输入的问题
#include "stdio.h"main()
{
FILE *fp;
char *p;
char a[80];
int i,x;
printf("写入选1,重新写入选2,添加选3\n");
scanf("%d",&x);
switch(x)
{
case 1:if((fp=fopen("d:\\myone.txt","w"))==NULL)
{
printf("cann't open myone\n");
exit(0);
}break;
case 2:if((fp=fopen("d:\\myone.txt","w"))==NULL)
{
printf("cann't open myone\n");
exit(0);
}break;
case 3:if((fp=fopen("d:\\myone.txt","a"))==NULL)
{
printf("cann't open myone\n");
exit(0);
}break;
default:printf("输入错误\n");
exit(0);
}
p=a;
printf("请输入一串字符:\n");
for(i=0;i<5;i++)//本意我是想输入5行字符的,但第一行却为空行。
{ gets(p);
while(*p!='\0')
{
fputc(*p,fp);
p++;
}
fputc('\n',fp);
}
fclose(fp);
}
但是当把这段注释时:
/*printf("写入选1,重新写入选2,添加选3\n");
scanf("%d",&x);
switch(x)
{
case 1:if((fp=fopen("d:\\myone.txt","w"))==NULL)
{
printf("cann't open myone\n");
exit(0);
}break;
case 2:if((fp=fopen("d:\\myone.txt","w"))==NULL)
{
printf("cann't open myone\n");
exit(0);
}break;
case 3:if((fp=fopen("d:\\myone.txt","a"))==NULL)
{
printf("cann't open myone\n");
exit(0);
}break;
default:printf("输入错误\n");
exit(0);
}*/
添加上
if((fp=fopen("d:\\myone.txt","w"))==NULL)
{
printf("cann't open myone\n");
exit(0);
}
却能实现了``???
为什么呢?