C语言-文件操作1

前言

学习一下C语言读取文件.

示例代码

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
	FILE *pWriter = fopen("hello.txt", "w");                //打开或新建hello.txt
	if (pWriter == NULL)
	{
		printf("创建或打开文件失败!");
		return 0;
	}

	fputs("hello world!", pWriter);				//在pWriter文件流写入内容"hello world!"

	fclose(pWriter);					//关闭文件,从缓冲区将内容写入到文件中
	system("pause");
	return 0;
}

fopen的几种模式

文件的使用方式

含义

r/rb(只读)

为输入打开一个文本或二进制文件

w/wb(只写)

为输出打开或新建一个文本或二进制文件

a/ab(追加)

在文本或二进制文件尾部追加数据

r+/rb+(读写)

读或写打开一个文本或二进制文件

w+/wb+(读写)

读或写新建一个文本或二进制文件

a+/ab+(读写)

读或写 打开或新建一个文本或二进制文件

使用fopen,一定要记得使用fclose.

个人能力有限,如果您发现有什么不对,请私信我

如果您觉得对您有用的话,可以点个赞或者加个关注,欢迎大家一起进行技术交流