C语言 文件函数

目录

[1. 文件的打开和关闭](#1. 文件的打开和关闭)

[2. 文件的顺序读写](#2. 文件的顺序读写)

[2.1 顺序读写函数介绍](#2.1 顺序读写函数介绍)

2.2读文件(读文件只能读一次)

2.3写文件

[3. 文件的随机读写](#3. 文件的随机读写)

[3.1 fseek](#3.1 fseek)

[3.2 ftell](#3.2 ftell)

[3.3 rewind](#3.3 rewind)

4.文件读取结束的判定

[4.1 被错误使误的 feof](#4.1 被错误使误的 feof)


我对读写的理解:(从文件里)读 --get,read,scanf ,写(到文件里) --put,write,printf

1. 文件的打开和关闭

文件在读写之前应该先打开文件 ,在使用结束之后应该关闭文件

ANSI C 规定使用 fopen 函数来打开文件, fclose 来关闭文件。

objectivec 复制代码
//打开⽂件
FILE* fopen(const char* filename, const char* mode);
//关闭⽂件
int fclose(FILE* stream);

**注意:**相对路径(若没有A.txt,直接在源文件所在文件夹中创建A.txt) :文件名.文件类型;如:fopen("A.txt","w");(访问 源文件所在文件夹中的A.txt)

绝对路径 :文件路径+文件名.文件类型;如:fopen("D:\\code\\A.txt","w");(\\防止出现转义字符)

mode表示文件的打开模式,下面都是文件的打开模式:

2. 文件的顺序读写

2.1 顺序读写函数介绍

2.2读文件(读文件只能读一次)

读C.txt文件:

objectivec 复制代码
#include <stdio.h>//读文件--"r",fgetc,fgets,fscanf

int main()
{
	FILE* pf = fopen("C.txt", "r");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}

	//int ch = 0;
	//while ((ch = fgetc(pf)) != EOF)//int fgetc ( FILE * stream );
	//{
	//	printf("%c", ch);
	//}

	//char arr[20] = { 0 };
    //fgets(arr, 8, pf);//char * fgets ( char * str, int num, file * stream );
    //fgets读7个字符+'\0'
    //printf("%s", arr);

	//char arr[20] = { 0 };
	//fscanf(pf, "%s", arr);//int fscanf ( FILE * stream, const char * format, ... );
	//printf("%s", arr);

	fclose(pf);
	pf = NULL;
	return 0;
}
objectivec 复制代码
#include <stdio.h>//读文件--"rb",fread

int main()
{
	FILE* pf = fopen("C.txt", "rb");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}

	char arr[20] = { 0 };
	fread(arr, sizeof(arr[0]), 4, pf);//size_t fread(void* ptr, size_t size, size_t count, FILE* stream);
	int i = 0;
	for (i = 0; i < 4; i++)
	{
		printf("%c", arr[i]);
	}
 
	fclose(pf);
	pf = NULL;
	return 0;
}

2.3写文件

要在C.txt文件中写入:

objectivec 复制代码
#include <stdio.h>//写文件--"w",fputc,fputs,fprintf

int main()
{
	FILE* pf = fopen("C.txt", "w");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}

	//fputc('c', pf);//int fputc ( int character, FILE * stream );
	//fputc('f', pf);

	//fputs("cf", pf);//int fputs ( const char * str, FILE * stream );

	//fprintf(pf, "%s", "cf");//int fprintf ( FILE * stream, const char * format, ... );

	fclose(pf);
	pf = NULL;
	return 0;
}
objectivec 复制代码
#include <stdio.h>//写文件--"wb",fwrite

int main()
{
	FILE* pf = fopen("C.txt", "wb");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}

	char arr[] = "cfg";
	fwrite(arr, sizeof(arr[0]), 2, pf);//size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );

	fclose(pf);
	pf = NULL;
	return 0;
}

"a"和"w"类似,只是"a"是在后面追加数据 ,"w"会把之前的数据覆盖重新写数据

注意:**字符 一律以ASCII**形式存储,数值型数据既可以用ASCII形式存储,也可以使用二进制形式存储,所以"rb"可以在文本文件中读出正确的字符,"rw"可以将真确的字符写到文本文件中

3. 文件的随机读写

3.1 fseek

根据文件指针的位置和偏移量来定位文件指针。

objectivec 复制代码
int fseek ( FILE * stream, long int offset, int origin );

偏移量 起始位置

SEEK_SET 文件的起始位置

SEEK_CUR 文件指针当前位置

SEEK_END 文件末尾

3.2 ftell

返回 文件指针 相对于 起始位置偏移量

objectivec 复制代码
 long int ftell ( FILE * stream );

3.3 rewind

文件指针 的位置回到文件的起始位置

objectivec 复制代码
void rewind ( FILE * stream );

例子:

objectivec 复制代码
#include <stdio.h>//fseek,ftell,rewind

int main()
{
	FILE* pf = fopen("C.txt", "r");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	printf("%c", fgetc(pf));//a
	fseek(pf, 4, SEEK_CUR);//int fseek ( FILE * stream, long int offset, int origin );
	printf("%c", fgetc(pf));//f

	printf("%d", ftell(pf));//6//long int ftell ( FILE * stream );

	rewind(pf);
	printf("%c", fgetc(pf));//a//void rewind ( FILE * stream );

	fclose(pf);
	pf = NULL;
	return 0;
}

4.文件读取结束的判定

4.1 被错误使误的 feof

**牢记:**在文件读取过程中,不能用feof函数的返回值 直接来判断文件的是否结束

feof 的作用是:当文件读取结束的时候,判断是读取结束的原因是否是:遇到文件尾结束。

  1. 文本文件读取是否结束,判断返回值是否为 EOF ( fgetc ),或者 NULL ( fgets )

  2. 二进制文件的读取结束判断,判断返回值是否小于实际要读的个数。如:fread

相关推荐
代码雕刻家2 小时前
数据结构-3.9.栈在递归中的应用
c语言·数据结构·算法
Kalika0-04 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
代码雕刻家4 小时前
课设实验-数据结构-单链表-文教文化用品品牌
c语言·开发语言·数据结构
龙图:会赢的4 小时前
[C语言]--编译和链接
c语言·开发语言
Cons.W6 小时前
Codeforces Round 975 (Div. 1) C. Tree Pruning
c语言·开发语言·剪枝
挥剑决浮云 -6 小时前
Linux 之 安装软件、GCC编译器、Linux 操作系统基础
linux·服务器·c语言·c++·经验分享·笔记
一颗星星辰7 小时前
C语言 | 第十章 | 函数 作用域
c语言·开发语言
꧁༺❀氯ྀൢ躅ྀൢ❀༻꧂7 小时前
实验4 循环结构
c语言·算法·基础题
从0至17 小时前
力扣刷题 | 两数之和
c语言·开发语言