文件读写(一)

目录

                                      文件读写之C语言

读写字符

读写字符串

读取数据块

其他常用


                                      文件读写之C语言

 

 

读写字符

	//读取字符
	FILE* pFile = NULL;
	if (NULL != (pFile = fopen(strFile1, "r")))
	{
		int getChar = fgetc(pFile);
		//未读取成功或读取到文件末尾时,返回EOF
		if (EOF != getChar)
		{
			printf("%c\n", getChar);
		}
		fclose(pFile);
	}
	/*
	while (EOF != (getChar = fgetc(pFile)))
	{
	//可循环一个一个字节读取,直到读取完毕
	}
	*/
	//写入字符
	FILE* pFile = NULL;
	if (NULL != (pFile = fopen(strFile1, "a+")))
	{
		int putChar = fputc('a', pFile);
		//失败时,返回EOF,否则返回被写入字符
		if (EOF != putChar)
		{
			printf("%c写入成功\n", putChar);
		}
		fclose(pFile);
	}

读写字符串

	//读取字符串
	FILE* pFile = NULL;
	if (NULL != (pFile = fopen(strFile1, "a+")))
	{
		char strBuf[256] = { 0 };
		//读取成功,返回指向strBuf的指针,否则返回NULL
		//读取到文件尾或出错,都会返回NULL
		//碰到'\n'就会停止读取,同时在读取的字符串后加'\0'
		if (NULL != fgets(strBuf, sizeof(strBuf), pFile))
		{
			printf("%s\n", strBuf);
		}
		/*
		while (!feof(pFile) && NULL!=fgets(strBuf,sizeof(strBuf),pFile))
		{
		//一行一行读取
		}
		*/

		fclose(pFile);
	}
	//写入字符串
	FILE* pFile = NULL;
	if (NULL!= (pFile = fopen(strFile1, "a+")))
	{
		char strBuf[256] = {"hello c++" };
		//写入成功返回一个非负值,否则返回EOF
		if (EOF != fputs(strBuf, pFile))
		{
			printf("写入成功\n");
		}

		fclose(pFile);
	}
	//格式化读取字符串
	FILE* pFile = NULL;
	if (NULL!= (pFile = fopen(strFile1, "w+")))
	{
		char strBuf1[256] = { 0 }, strBuf2[256] = { 0 };
		int iNum = 0;
		if (EOF != fputs("wolcom to 2019", pFile))
		{
			rewind(pFile);
			//成功时返回成功赋值的个数,否则返回EOF
			//这种格式化读取,需要知道文件数据格式
			//只能读取ASII编码格式的文件
			if (EOF != fscanf(pFile, "%s %s %d", strBuf1, strBuf2, &iNum))
			{
				printf("读取成功\n");
			}
			/*
				while (!feof(pFile))
				{
					//一行一行读取
				}
			*/
		}
	//格式化写入字符串
	FILE* pFile = NULL;
	if (NULL!= (pFile = fopen(strFile1, "w+")))
	{
		//成功时,返回写入字符总数,否则返回一个负数
		if (fprintf(pFile,"%s %s %d","welcom","to",2019)>0)
		{
			printf("写入成功\n");
		}
		fclose(pFile);
	}

读取数据块

struct Student 
{
	int m_id;
	char m_name[64];
	double m_score;
	Student();
	Student(int id, char* name, double score);
};
Student::Student()
{
	m_id = 0;
	memset(m_name, 0, sizeof(m_score));
	m_score = 0.0;
}
Student::Student(int id, char* name, double score)
{
	m_id = id;
	_snprintf_s(m_name, sizeof(m_name), sizeof(m_name) - 2, "%s", nullptr==name? "":name);
	m_score = score;
}
	//模块化读取
	FILE* pFile = NULL;
	if (NULL!= (pFile = fopen(strFile1, "r")))
	{
		Student stuMy;
		//不成功或者读到文件尾,返回0
		if (0 != fread(&stuMy, sizeof(Student), 1, pFile))
		{
			printf("模块读取成功\n");
		}
		fclose(pFile);
	}
	//模块化写入
	FILE* pFile = NULL;
	if (NULL!= (pFile = fopen(strFile1, "w+")))
	{
		Student stuMy(1, "wxd", 99.5);
		//返回写入的模块个数
		if (1 == fwrite(&stuMy, sizeof(Student), 1, pFile))
		{
			printf("模块写入成功\n");
		}
		fclose(pFile);
	}

其他常用

	/*
	void rewind(FILE *stream)  
		将文件指针定位在文件开头 

	int fseek(FILE *stream, long offset, int fromwhere); 
		以fromwhere为基准,文件指针偏移offset个字节的位置
		fromwhere:
			SEEK_SET: 文件开头
			SEEK_CUR: 当前位置
			SEEK_END: 文件结尾

	long int ftell(FILE *stream)
		得到文件当前指针的位置

	int setvbuf(FILE *stream, char *buffer, int mode, size_t size)
		定义stream如何缓冲
		buffer:分配给用户的缓冲,如果为NULL时,系统自动分配一个指定大小的缓冲
		mode:缓冲模式
			_IOFBF: 全缓冲,数据在缓冲填满时被一次性写入
			_IOLBF : 行缓冲,数据在遇到换行符或者在缓冲填满时被写入
			_IONBF : 无缓冲,不使用缓冲,随时写入
		size : 缓冲大小(字节)

	void setbuf(FILE *stream, char *buffer)
		给stream设置缓冲区
		相当于setvbuf(stream,buffer,buffer? _IOFBFL:_IONBF, buffersize )

	int flushall(); 清除所有打开文件的缓冲区
	int fflush(FILE *stream) 将缓冲区内容刷新到stream中
	*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值