网站首页

人工智能P2P分享搜索全网发布信息网站地图标签大全

当前位置:诺佳网 > 电子/半导体 > 嵌入式技术 >

C语言_文件IO操作函数总结

时间:2022-08-14 09:53

人气:

作者:admin

标签: 函数  C语言  文件系统 

导读:当前文章涉及C语言文件操作相关知识点。列出最常见的文件操作函数、fopen、fread、fwrite、fclose 等。通过几个常见需求,写出例子理解文件操作函数的用法。...

1. 文件IO总结

文件IO操作:  对文件系统里的文件进行:  打开、创建、读、写、关闭等运用。
C语言下标准文件IO接口(函数): 
(1)头文件:  stdio.h    输入输出函数:  printf 、scanf
(2)相关函数:  fopen、fread、fwrite、fclose
2.1 标准文件操作有两套函数:
1.标准C语言下的文件操作接口。fopen系列
常用于:  对普通文件的读写。
2.Linux操作系统下的文件操作接口。open系列
常用于:  对设备文件进行读写。 (鼠标、键盘、声卡、..)

2. C语言标准文件操作接口

2.1 最常用的4个函数

#include 
//打开文件
FILE *fopen(const char *path, const char *mode); 
//读文件
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
//写文件
size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);
//关闭文件
int fclose(FILE *fp);

2.3 写函数的基本运用

#include 
#include 
#include 

int main()
{
	FILE *file;
	int cnt;
	/*1. 打开文件*/
	file=fopen("D:/123.txt","a+b");
	if(file==NULL)
	{
		printf("文件打开失败!\n");
		return -1;
	}
	/*2. 写数据*/
	cnt=fwrite("1234567890",1,10,file);
	
    /*3. 关闭文件*/
	fclose(file);

	printf("cnt=%d\n",cnt);
	return 0;
}

2.4 读函数基本运用

#include 
#include 
#include 

int main()
{
	FILE *file;
	int cnt;
	char buff[100];

	/*1. 打开文件*/
	file=fopen("D:/123.txt","rb"); //malloc
	if(file==NULL)
	{
		printf("文件打开失败!\n");
		return -1;
	}
	/*2. 写数据*/
	cnt=fread(buff,1,100,file);
	
    /*3. 关闭文件*/
	fclose(file);  //free 

	buff[cnt]='\0';
	printf("%s\n",buff);
	printf("cnt=%d\n",cnt);
	return 0;
}

2.5 文件指针位置偏移 (自动向后偏移)

#include 
#include 
#include 
int main()
{
	FILE *file;
	int cnt;
	char data;

	/*1. 打开文件*/
	file=fopen("D:/123.txt","rb"); //malloc
	if(file==NULL)
	{
		printf("文件打开失败!\n");
		return -1;
	}
	/*2. 读数据---验证文件指针是否可否自动向后偏移*/
	cnt=fread(&data,1,1,file);
	printf("data=%c\n",data);
	cnt=fread(&data,1,1,file);
	printf("data=%c\n",data);
	cnt=fread(&data,1,1,file);
	printf("data=%c\n",data);
	cnt=fread(&data,1,1,file);
	printf("data=%c\n",data);
	cnt=fread(&data,1,1,file);
	printf("data=%c\n",data);

    /*3. 关闭文件*/
	fclose(file);  //free 
	return 0;
}

2.6 设置文件指针位置

#include 
#include 
#include 
int main()
{
	FILE *file;
	int cnt;
	char data;

	/*1. 打开文件*/
	file=fopen("D:/123.txt","rb"); //malloc
	if(file==NULL)
	{
		printf("文件打开失败!\n");
		return -1;
	}
	/*2. 偏移文件指针*/
	fseek(file,5,SEEK_SET);

	/*3. 读数据---验证文件指针是否可否自动向后偏移*/
	cnt=fread(&data,1,1,file);
	printf("data=%c\n",data);

    /*4. 关闭文件*/
	fclose(file);  //free 
	return 0;
}

2.7 以上午所学的函数,如何判断文件读完了?到文件结尾?


#include 
#include 
#include 
int main()
{
	FILE *file;
	int cnt;
	char data;

	/*1. 打开文件*/
	file=fopen("D:/123.txt","rb"); //malloc
	if(file==NULL)
	{
		printf("文件打开失败!\n");
		return -1;
	}
	/*2. 偏移文件指针*/
	fseek(file,5,SEEK_SET);

	/*3. 读数据---验证文件指针是否可否自动向后偏移*/
	while(1)
	{
		cnt=fread(&data,1,1,file);
		if(cnt!=1)break;
		printf("data=%c\n",data);
	}
    /*4. 关闭文件*/
	fclose(file);  //free 
	return 0;
}

2.8 文件读写结构体数据

//写结构体数据
#include 
#include 
#include 
struct MyStruct
{
	int a;
	int b;
	char c[100];
};

int main()
{
	FILE *file;
	int cnt;
	struct MyStruct stu={666,888,"C语言文件操作学习"};

	/*1. 打开文件*/
	file=fopen("D:/123.txt","wb"); 
	if(file==NULL)
	{
		printf("文件打开失败!\n");
		return -1;
	}

	/*2. 读数据*/
	cnt=fwrite(&stu,1,sizeof(struct MyStruct),file);
	printf("cnt=%d\n",cnt);

    /*3. 关闭文件*/
	fclose(file);  //free 
	return 0;
}

//读结构体数据
#include 
#include 
#include 
struct MyStruct
{
	int a;
	int b;
	char c[100];
};

int main()
{
	FILE *file;
	int cnt;
	struct MyStruct stu;

	/*1. 打开文件*/
	file=fopen("D:/123.txt","rb"); 
	if(file==NULL)
	{
		printf("文件打开失败!\n");
		return -1;
	}

	/*2. 读数据*/
	cnt=fread(&stu,1,sizeof(struct MyStruct),file);
	printf("cnt=%d\n",cnt);

	printf("%d,%d,%s\n",stu.a,stu.b,stu.c);
    /*3. 关闭文件*/
	fclose(file);  //free 
	return 0;
}

2.9 文件操作的作业练习

1.  学习文件基本读写使用
2.  编写文件拷贝程序。 实现文件拷贝。
3.  文件加密解密实现。 需要编写一个菜单。
4.   完善学生管理系统。
需要将所有学生信息保存到文件里,完善功能。

审核编辑:汤梓红
温馨提示:以上内容整理于网络,仅供参考,如果对您有帮助,留下您的阅读感言吧!
相关阅读
本类排行
相关标签
本类推荐

CPU | 内存 | 硬盘 | 显卡 | 显示器 | 主板 | 电源 | 键鼠 | 网站地图

Copyright © 2025-2035 诺佳网 版权所有 备案号:赣ICP备2025066733号
本站资料均来源互联网收集整理,作品版权归作者所有,如果侵犯了您的版权,请跟我们联系。

关注微信