您的位置:首页 > 运维架构

使用fopen()函数以不同模式打开磁盘文件(里面有个bug)

2015-10-19 00:00 411 查看
// openfiles.c -- 演示fopen()函数

#include <stdio.h>
#include <stdlib.h>
int main(void){
FILE *fp;
char ch, filename[40], mode[4];
while(1){
/*输入文件名和模式*/
puts("\nEnter a filename: ");
gets(filename);
puts("\nEnter a mode (max 3 characters): ");
gets(mode);

/*尝试打开文件*/
if((fp = fopen(filename, mode)) != NULL){
printf("\nSuccessful opening %s in mode %s.\n", filename,mode);
fclose(fp);
puts("Enter x to exit, any other to continue.");
if((ch = getc(stdin)) == 'x')
break;
else
continue;
} else {
fprintf(stderr, "\nError opening file %s in mode %s.\n", filename, mode);
puts("Enter x to exit, any other to try again.");
if((ch = getc(stdin)) == 'x')
break;
else
continue;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: