天天看点

C语言文件操作<二>前言总结

(文章目录)

前言

接上一篇 C语言文件操作<一>

一、文件的顺序读写函数

fgetc和fputc

  • fgetc

    字符输入函数 适用于所有输入流

  • fputc

    字符输出函数 适用于所有输出流

int fgetc( FILE *stream );
int fputc( int c, FILE *stream );
           
  • fputc 写文件
int main()
{
	FILE* pfWrite = fopen("test.txt", "w");
	if (pfWrite == NULL)//检验
	{
		printf("%s\n", strerror(errno));//如果写文件错误打印错误原因
		return 0;
	}
	//写文件
	fputc("a", pfWrite);
	fputc("b", pfWrite);
	fputc("c", pfWrite);
	//关闭文件
	fclose(pfWrite);
	pfWrite == NULL;
	return 0;
}
           
  • fgetc 读文件
int main()
{
	FILE* pfRead = fopen("test.txt", "r");
	if (pfRead == NULL)
	{
		printf("%s\n", strerror(errno));
		return 0;
	}
	//读文件
	printf("%s", fgetc(pfRead));//a
	printf("%s", fgetc(pfRead));//b
	printf("%s", fgetc(pfRead));//c
	//关闭文件
	fclose(pfRead);
	pfRead == NULL;
	return 0;
}
           

fgets和fputs

  • fges 文本行输入函数 适用于所有输入流
  • fputs 文本行输入函数 适用于所有输出流

两个函数的功能:

//Get a string from a stream. 从流中获取字符串。
char *fgets(char *string, int n, FILE *stream);
//Write a string to a stream. 将字符串写入流。
int fputs(const char *string, FILE *stream);
           

用fgets函数读取一行:

int main()
{
	char buff[100] = { 0 };
	FILE* pf = fopen("test.txt", "r");
	if (pf == NULL)
	{
		return 0;
	}
	//读文件
	fgets(buff, 100, pf);
	printf("%s", buff); //注意buff里面本身就有一个换行符 (文件里面末尾有换行符)

	//打开成功,读文件,关闭文件
	fclose(pf);
	pf == NULL;
	return 0;
}
           

读取一行打印到显示器上 如图:

C语言文件操作&lt;二&gt;前言总结

用fputs函数写一行:

int main()
{
	char buff[100] = { 0 };
	FILE* pf = fopen("test.txt", "w");
	if (pf == NULL)
	{
		return 0;
	}
	//写文件
	fputs("hello\n", pf);
	fputs("word\n", pf);

	//打开成功,写文件,关闭文件
	fclose(pf);
	pf = NULL;
	return 0;
}
           

用fputs 写一行到文件里面:如下:

C语言文件操作&lt;二&gt;前言总结

fgets和fputs也可以操作键盘和屏幕

C语言文件操作&lt;二&gt;前言总结

键盘输入abcd 回车 屏幕打印 abcd

fscanf 和 fprintf

  • fscanf 格式化输入函数 适用于所有输入流
  • fprintf 格式化输出函数 适用于所有输出流

简单对比 scanf 和 fscanf,printf 和 fprintf,用法都非常相似

int scanf( const char *format [,argument]... );
int fscanf( FILE *stream, const char *format [, argument ]... );

int printf( const char *format [, argument]... );
int fprintf( FILE *stream, const char *format [, argument ]...);
           

fprintf 写文件

struct S
{
	int n;
	float score;
	char arr[10];
};
int main()
{
	struct S s = { 100, 3.14, "abc" };
	FILE* pf = fopen("test.txt", "w");
	if (pf == NULL)
	{
		return 0;
	}
	//格式化的形式写文件
	fprintf(pf, "%d %f %s", s.n, s.score, s.arr);

	fclose(pf);
	pf = NULL;
	return 0;
}
           
C语言文件操作&lt;二&gt;前言总结

fscanf 读文件

struct S
{
	int n;
	float score;
	char arr[10];
};
int main()
{
	struct S s = { 100, 3.14, "abc" };
	FILE* pf = fopen("test.txt", "w");
	if (pf == NULL)
	{
		return 0;
	}
	//格式化的形式输入数据
	fscanf(pf, "%d %f %s", s.n, s.score, s.arr);
	fclose(pf);
	pf = NULL;
	return 0;
}
           

当然fscanf和fprintf函数同样可以运用于标准的输入输出流(stdin,stdout)

对比一组函数

  • scanf/fscanf/sscanf
  • ptintf/fprintf/sprintf

scanf/printf:是针对标准输入流/标准输出流的 格式化输入/输出语句

fscnaf/fprintf:是针对所有输入流/所有输出流的 格式化输入/输出语句

sscanf/sprintf:sscanf是从字符串中读取格式化的数据。sprintf是把格式化数据输出成(存储到)字符串

fraed 和 fwriite

  • fread 二进制输入函数 适用于 文件
  • fwrite 二进制输出函数 适用于 文件
size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );
size_t fread( void *buffer, size_t size, size_t count, FILE *stream );

struct S{
	char name[20];
	int age;
	double score;	
};
int main()
{
	struct S s = { "张三", 20, 55.6 };
	FILE* pf = fopen("test.txt", "wb");
	if (pf == NULL)
	{
		return 0;
	}
	//二进制形式写文件
	fwrite(&s, sizeof(struct S), 1, pf);
	
	fclose(pf);
	pf = NULL;
	return 0;
}
           

fread 读文件 ,fwrite 写文件 。两个函数用法类似

二、文件的随机读写函数

前面介绍的函数都是顺序读写函数 ,有时候我们需要从某一个位置开始读写函数。下面的这些函数可以实现其功能。

fseek 函数

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

Moves the file pointer to a specified location.
int fseek( FILE *stream, long offset, int origin );
//三个参数所指的内容:pf    偏移量     文件指针的当前位置

int main()
{
	FILE* pf = fopen("test.txt", "r");
	if (pf == NULL)
	{
		return 0;
	}
	//定位文件指针
	fseek(pf,2,SEEK_CUR);//偏移量为2
	//读取文件
	int ch = fgetc(pf);
	prinf("%c\n",ch);
	fclose(pf);
	pf = NULL;
	return 0;
}
           
C语言文件操作&lt;二&gt;前言总结

如图从文件指针的当前位置 a 前面开始偏移量为2,打印c

  • SEEK_CUR

    Current position of file pointer (文件指针的当前位置)

  • SEEK_END (文件的末尾位置)

    End of file

  • SEEK_SET (文件起始位置)

    Beginning of file

三、关于文件结束的判定

我们知道:EOF --> end of file 文件结束的标志

如果一个文件里面什么都没有我们打开文件读到的就是 “-1”,文件结束的位置有一个EOF存在。

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

    而是应用于当文件读取结束的时候,判断是读取失败结束,还是遇到文件尾EOF使文件结束。

  1. 文本文件是否读取结束,判断返回值是否为EOF或则NULL

    例如:fgetc判断是否为EOF fgets判断返回值是否为NULL

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

    例如:fread 判断返回值是否小于实际要读的个数

总结