这里写代码片
//文件的读取
# include<stdio.h>
#include<io.h>
void main()
{
FILE*fp;
//if((fp=fopen("E:\计算机软件\awe.txt","r"))==NULL)//路径书写错误
if((fp=fopen("E:\\计算机软件\\awe.txt","r"))==NULL)
{
printf("Cannot open this file\n");
}
else
{
printf("This file is OK\n");
}
if(fclose(fp)) printf("file close error!\n");
}
文件的读取应该注意和明确以下内容:
- 文件的命名可以是中文也就可以是英文
- 如果文件是txt格式,命名时不要加后缀名,但是C语言文件的名称中则需要加后缀名
- 文件的路径可以出现中文
- c语言中文件的路径遇到”\”要记得转义为双杠”\”
- 此函数也可以读取docx类型的文件