0
点赞
收藏
分享

微信扫一扫

【深度学习】—— 神经网络介绍

天悦哥 2024-06-11 阅读 10

目录

前言

上篇文章中我们初步了解了文件的相关信息,文件的打开和关闭,以及文件的随机读写等
本篇文章将详细介绍一些文件顺序读写函数的作用、特点和用法,使我们更加方便地操作文件,还会讲到如何判定文件的结束等,内容可能有点多,请耐心看完哦


四、文件的顺序读写

4.1 顺序读写函数介绍

函数名功能适用于
fgetc字符输入函数所有输入流
fputc字符输出函数所有输出流
fgets文本行输入函数所有输入流
fputs文本行输出函数所有输出流
fscanf格式化输出函数所有输入流
fprintf格式化输出函数所有输出流
fread二进制输入文件输入流
fwrite二进制输出文件输出流

4.1.1 fputc

fputc函数的原型如下:

int fputc( int ch, FILE *stream );
  • ch:要写入的字符
  • stream: 输出流

开始时在当前工程目录底下创建一个文本文档,存入数据:
请添加图片描述

运行下面的代码:

#include <stdio.h>

int main()
{
	//打开文件
	FILE* pf = fopen("test.txt", "w");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}

	//写文件
	int i = 0;
	for (i = 'a'; i <= 'z'; i++)
	{
		//fputc函数一次只能写入一个字符
		fputc(i, pf);
	}

	//关闭文件
	fclose(pf);
	pf = NULL;

	return 0;
}

运行上面的代码后查看文档,可以看到里面的内容已经被修改

请添加图片描述


4.1.2 fgetc

fgetc函数原型如下:

int fgetc( FILE *stream );
  • stream:读取字符的来源

运行下面的代码:

#include <stdio.h>

int main()
{
	//打开文件
	FILE* pf = fopen("test.txt", "r");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}

	//读文件
	int ch = 0;//注意:为处理 EOF 需要 int 而非 char
	while ((ch = fgetc(pf)) != EOF)
	{
		printf("%c ", ch);
	}

	//关闭文件
	fclose(pf);
	pf = NULL;

	return 0;
}

运行成功,在终端上打印出 ‘a’ ~ ‘z’:

请添加图片描述

#include <stdio.h>

int main()
{
	int ch = 0;
	ch = fgetc(stdin);//从键盘(标准输入流)上读取
	fputc(ch, stdout);//将字符输出(写)到屏幕(标准输出流)
	return 0;
}

请添加图片描述


4.1.3 fputs

fputs函数原型如下:

int fputs( const char *str, FILE *stream );

运行下面的代码:

#include <stdio.h>

int main()
{
	//打开文件
	FILE* pf = fopen("test.txt", "w");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	//写文件
	fputs("Are you ok?", pf);

	//关闭文件
	fclose(pf);
	pf = NULL;

	return 0;
}

运行成功后查看文档,内容已经被重写:

请添加图片描述

fputs函数在写入字符串的时候是不主动换行的

#include <stdio.h>

int main()
{
	//打开文件
	FILE* pf = fopen("test.txt", "w");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	//写文件
	fputs("Are you ok?", pf);
	fputs("What can I say? man.", pf);

	//关闭文件
	fclose(pf);
	pf = NULL;

	return 0;
}

在这里插入图片描述


4.1.4 fgets

fgets函数原型如下:

char* fgets( char *str, int count, FILE *stream );
  • str:指向char型数组元素的指针
  • count:写入的最大字符数(典型的为 str 的长度)
  • stream:读取数据来源的文件流

fgets函数的作用:

  • 从给定文件流读取最多count-1个字符并将它们存储于str所指向的字符数组
  • 若文件尾出现或发现换行符则终止分析,后一情况下 str 将包含一个换行符
  • 若读入字符且无错误发生,则紧随str的最后一个字符后写入空字符'\0'

test.txt文档中的内容改为“abcdefghijklmnopq”:

在这里插入图片描述

调试下面的代码:

#include <stdio.h>

int main()
{
	//打开文件
	FILE* pf = fopen("test.txt", "r");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	//读文件
	char str[20] = "xxxxxxxxxxxx";
	fgets(str, 5, pf);
	printf("%s\n", str);

	//关闭文件
	fclose(pf);
	pf = NULL;

	return 0;
}

在这里插入图片描述

可以看到,虽然函数fgets确实在数组str中存入了5个字符,但是只读取了文档test.txt实际的4个字符存入数组str中,还有一个是字符‘\0’

test.txt文档中的内容改为:
在这里插入图片描述

调试下面的代码:

#include <stdio.h>

int main()
{
	//打开文件
	FILE* pf = fopen("test.txt", "r");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	//读文件
	char str[20] = "xxxxxxxxxxxx";
	fgets(str, 10, pf);
	printf("%s\n", str);

	//关闭文件
	fclose(pf);
	pf = NULL;

	return 0;
}

在这里插入图片描述

当然不管哪种情况最后都会补‘\0’

同样的,fgetsfputs也适用所有输入流和所有输出流,当然也包括标准输入流stdin和标准输出流stdout

#include <stdio.h>

int main()
{
	char str[20] = { 0 };
	fgets(str, 20, stdin);
	fputs(str, stdout);
	return 0;
}

在这里插入图片描述


4.1.5 fprintf

fprintf函数原型如下:

int fprintf( FILE *stream, const char *format, ... );

对比printf

int printf( const char *format, ... );

可以看到fprintf函数比printf多了一个参数——文件指针
其中 ...表示可变参数列表,所以函数fprintf也可以有若干个参数

运行下面的代码:

#include <stdio.h>

struct S
{
	char name[20];
	int age;
	double weight;
};

int main()
{
	struct S s = { "xiaoshuai", 25, 75.3 };
	//打开文件
	FILE* pf = fopen("test.txt", "w");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}

	//写文件
	fprintf(pf, "%s\n%d\n%.1lf\n", s.name, s.age, s.weight);

	//关闭文件
	fclose(pf);
	pf = NULL;

	return 0;
}

请添加图片描述

可以看到,文档的内容已经被重写


4.1.6 fscanf

fscanf函数的原型如下:

int fscanf( FILE *stream, const char *format, ... );

对比scanf函数:

int scanf( const char *format, ... );

也是多了一个文件指针参数

运行下面的代码:

#include <stdio.h>

struct S
{
	char name[20];
	int age;
	double weight;
};

int main()
{
	struct S s = { 0 };
	//打开文件
	FILE* pf = fopen("test.txt", "r");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}

	//读文件
	fscanf(pf, "%s %d %lf\n", s.name, &s.age, &s.weight);
	//从文件中读取的信息存到结构体s中
	//注意:从文件中读的时候不要用  %.1lf
	//s.name是数组名不需要加取地址操作符
	printf("%s\n%d\n%.1lf\n", s.name, s.age, s.weight);
	//打印在屏幕上
	
	//关闭文件
	fclose(pf);
	pf = NULL;

	return 0;
}

请添加图片描述

当然也可以用fprintf打印:

在这里插入图片描述


4.1.7 sprintf(操作的不是文件)

sprintf函数原型如下:

int sprintf( char *buffer, const char *format, ... );

运行下面的代码:

#include <stdio.h>

struct S
{
	char name[20];
	int age;
	double weight;
};

int main()
{
	char str[100] = { 0 };
	struct S s = { "xiaomei", 24, 55.2 };
	sprintf(str, "%s %d %.1lf", s.name, s.age, s.weight);
	printf("%s\n", str);
	return 0;
}

在这里插入图片描述


4.1.8 sscanf(操作的不是文件)

sscanf函数的原型如下:

int sscanf( const char *buffer, const char *format, ... );

运行下面的代码:

#include <stdio.h>

struct S
{
	char name[20];
	int age;
	double weight;
};

int main()
{
	char str[100] = { 0 };
	struct S s = { "xiaomei", 24, 55.2 };
	//将结构体变量s中格式化的数据转化为字符串存入字符数组str中
	sprintf(str, "%s %d %.1lf", s.name, s.age, s.weight);
	//printf("%s\n", str);

	//临时变量
	struct S tmp = { 0 };

	//将字符数组str中的数据格式化的存入结构体变量tmp中
	sscanf(str, "%s%d%lf", tmp.name, &tmp.age, &tmp.weight);
	fprintf(stdout, "%s %d %.1lf", tmp.name, tmp.age, tmp.weight);

	return 0;
}

请添加图片描述


  • scanf/printf针对标准输入流 / 标准输出流的格式化输入 / 输出函数
  • fscanf/fprintf针对所有输入流 / 所有输出流的格式化输入 / 输出函数
  • sscanf/sprintf将格式化的数据转换为字符串 / 将字符串转化为格式化的数据

4.1.9 fwrite

fwrite函数原型如下:

size_t fwrite( const void *buffer, size_t size, size_t count,FILE *stream );

fwrite函数的参数:

  • buffer:指向数组中要被写入的首个对象的指针
  • size:每个对象的大小
  • count:要被写入的对象数
  • stream:指向输出流的指针

运行下面的代码:

#include <stdio.h>

struct S
{
	char name[20];
	int age;
	double weight;
};

int main()
{
	struct S s = { "xiaochou", 23, 65.7 };
	//打开文件
	FILE* pf = fopen("test.txt", "wb");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	
	//写文件
	//以二进制的形式写到文件中
	fwrite(&s, sizeof(struct S), 1, pf);

	//关闭文件
	fclose(pf);
	pf = NULL;

	return 0;
}

请添加图片描述

运行成功后文档内是我们看不懂的二进制的信息


4.1.10 fread

fread函数的原型如下:

size_t fread( void *buffer, size_t size, size_t count,FILE *stream );

fread函数的参数:

  • buffer:指向要读取的数组中首个对象的指针
  • size:每个对象的字节大小
  • count:要读取的对象数
  • stream:读取来源的输入文件流

运行下面的代码:

#include <stdio.h>

struct S
{
	char name[20];
	int age;
	double weight;
};

int main()
{
	struct S s = { 0 };
	//打开文件
	FILE* pf = fopen("test.txt", "rb");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	
	//写文件
	//读取二进制的信息到文件中
	fread(&s, sizeof(struct S), 1, pf);
	//打印结构s的成员
	fprintf(stdout, "%s\n%d\n%.1lf\n", s.name, s.age, s.weight);

	//关闭文件
	fclose(pf);
	pf = NULL;

	return 0;
}

请添加图片描述

可以看到fread函数又将文件中的二进制信息读到了结构体中变成了我们可以看懂的信息


五、文件结束的判定

5.1 被错误使用的feof

文件读取结束有两个原因:

    1. 遇到文件结尾
    1. 遇到错误

feof函数的原型如下:

int feof( FILE *stream );

feof函数的返回值:若已抵达流尾则为非零值,否则为 ​0​

但是这个函数经常被用错,部分人以为feof函数的作用是判断文件读取是否结束,其实不是的


5.2 文本文件读取结束

文本文件读取是否结束,判断返回值:

  • fgetc:判断是否为EOF
  • fgets:判断是否为NULL

例如:

#include <stdio.h>

int main()
{
	//打开文件
	FILE* pf = fopen("test.txt", "r");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}

	//读文件
	int ch = 0;//注意:为处理 EOF 需要 int 而非 char
	while ((ch = fgetc(pf)) != EOF)
	{
		printf("%c ", ch);
	}

	//判断是什么原因结束的
	if (ferror(pf))
	{
		puts("I/O error when reading");
	}
	else if (feof(pf))
	{
		puts("End of file reached successfully");
	}

	//关闭文件
	fclose(pf);
	pf = NULL;

	return 0;
}

5.3 二进制文件读取结束

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

#include <stdio.h>

struct S
{
	char name[20];
	int age;
	double weight;
};

int main()
{
	struct S s = { 0 };
	//打开文件
	FILE* pf = fopen("test.txt", "rb");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}

	//读取二进制的信息到文件中
	size_t num = fread(&s, sizeof(struct S), 1, pf);
	if (num == 1)
	{
		puts("Array read successfully, contents:");
	}
	else if (feof(pf))
	{
		printf("Error reading test.bin: unexpected end of file\n");
	}
	else if (ferror(pf))
	{
		perror("Error reading test.bin");
	}
	//关闭文件
	fclose(pf);
	pf = NULL;

	return 0;
}

六、文件缓冲区

ANSIC标准采用“缓冲文件系统”处理数据文件的,所谓缓冲文件系统是指系统自动的在内存中为程序中每一个正在使用的文件开辟一块“文件缓冲区”。从内存向磁盘输出数据会先送到内存中的缓冲区,装满缓冲区后才一起送到磁盘上。

如果从磁盘向计算机读入数据,则从磁盘文件中读取数据输入到内存缓冲区,充满缓冲区后再逐个地将数据送到程序数据区(程序变量等),缓冲区的大小根据C编译系统决定。


总结

举报

相关推荐

0 条评论