C语言中文件的打开与关闭

时间:2022-09-02 23:18:15
这里写代码片
//文件的读取 
# 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");
} 

文件的读取应该注意和明确以下内容:

  1. 文件的命名可以是中文也就可以是英文
  2. 如果文件是txt格式,命名时不要加后缀名,但是C语言文件的名称中则需要加后缀名
  3. 文件的路径可以出现中文
  4. c语言中文件的路径遇到”\”要记得转义为双杠”\”
  5. 此函数也可以读取docx类型的文件