大方县中医院 人事科:C语言 FILE *fp(filename,mode)中的问题

来源:百度文库 编辑:神马品牌网 时间:2024/05/03 04:45:08
include<stdio.h>
#include<stdlib.h>
void main(){
FILE *fp;
char ch,filename[20];
printf("请输入要打开的文件:");
scanf("%s",filename);
fp=fopen(filename,"r+");
if(fp==NULL){
printf("此文件并不存在\n");
exit(0);
}
printf("正在打开文件\n");
ch=getchar();
while(ch!='#'){
fputc(ch,fp);
putchar(ch);
ch=getchar();
}
fclose(fp);

}

我在运行此程序,发现为什么文件不存在却一样执行,而不是说不存在。是不是在VC中“a"不是象在C中说的那样呀
不好意思,源程序中使用"a",不是使用的“r+",但我输入不存在的文件,却自己产生了此文件,郁闷

二楼的师兄,如果a代表不存在就新建,那w呢,怎么和我看的资料不同了

"r"

Opens for reading. If the file does not exist or cannot be found, the fopen call fails.

"w"

Opens an empty file for writing. If the given file exists, its contents are destroyed.

"a"

Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn’t exist.

"r+"

Opens for both reading and writing. (The file must exist.)

"w+"

Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.

"a+"

Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn’t exist.

a:代表如果文件不存在,则建立一个新文件