C语言——文件操作相关的函数初步认识

文件操作相关函数及其作用

函数名作用
fopen打开文件
fclose关闭文件
fgetc字符输入
fputc字符输出
fgets文本行输入
fputs文本行输出
fscanf格式化输入
fprintf格式化输出
fread二进制输入
fwrite二进制输出
fseek根据文件指针的位置和偏移量定位指针
ftell返回文件指针相对初始位置的偏移量
rewind让文件指针的位置回到文件的起始位置
feof文件的结束判定

文件的打开方式:
r:为了输入数据,打开一个已经存在的文本文件
“w”(只写) 为了输出数据,打开一个文本文件 建立一个新的文件
“a”(追加) 向文本文件尾添加数据
“rb”(只读) 为了输入数据,打开一个二进制文件
“wb”(只写) 为了输出数据,打开一个二进制文件 建立一个新的文件
“ab”(追加) 向一个二进制文件尾添加数据
“r+”(读写) 为了读和写,打开一个文本文件
“w+” (读写)为了读和写,建立一个新的文件
“a+” (读写)打开一个文件,在文件尾进行读写
“rb+” (读写)为了读和写打开一个二进制文件
“wb+”(读写)为了读和写,建立一个新的二进制文件
“ab+”(读写)打开一个文件,在二进制文件尾进行读写

函数名适用范围
fgetc所有输入流
fputc所有输出流
fgets所有输入流
fputs所有输出流
fscanf所有输出流
fprint所有输出流
fread文件
fwrite文件

文件函数的内容

1. fclose的函数体
int fclose(FILE* stream);
返回值类型: 整型
参数: 要关闭的流(文件)

2. fopen的函数体
FILE * fopen(const char* filename, const char* mode);
返回值类型: 文件指针类型
参数: 要打开的文件名,以什么模式打开。如果打开失败,返回NULL

3. ftell的函数体
long int ftell(FILE* stream);
返回值类型: 长整型
参数: 移到偏移量

4. fseek的函数体
int fseek(FILE* stream,long int offset,int origin);
返回值类型: 整型
参数: 从起始开始偏移

5. rewind的函数体
void rewind(FILE* stream);
返回值类型:
参数: 回到当前位置

6. fgetc的函数体
int fgetc(FILE *stream);
返回值类型: ASCII码值(整型)
参数: 读一个流的字符

7. fgets的函数体
char *fgets(char *string , int size, FILE *stream);
返回值类型: 字符指针(先判断返回值是否为EOF或NULL)
参数: 从流里读取最多个数的字符串

8. fputc的函数体
int fputc(int c, FILE *stream);
返回值类型: 整型
参数: 写一个字符到一个流(文件)

9. fputs的函数体
int fputs(const char *string, FILE *stream);
返回值类型: 整型
参数: 写一个字符串到流

10. fread的函数体(二进制的读)
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
返回值类型: 无符号的整型
参数: 读取流的数据的大小个数,存到某处

11. fwrite的函数体(二进制的写)
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
返回值类型: 无符号整型
参数: 从某处将含有多少字节数的数据的个数写进流

12. feof的函数体
int feof(FILE *stream);
返回值类型: 整型
参数: 判断getc是否为EOF,gets是否为NULL(正常结束)

13. fprintf的函数体
int fprintf(FILE* stream, const char* format,[argument]);
返回值类型: 整型
参数: 把数据以某特定的格式写入流

14. fscanf的函数体
int fscanf(FILE* stream,const char* format,[argument]);
返回值类型: 整型
参数: 把数据以某种特定的格式从流读出来

代码如下:

#include <stdio.h>
int main()
{
	int a = 10000;
	FILE* pf = fopen("test.txt", "wb");
	fwrite(&a, 4, 1, pf);//二进制的形式写到文件中
	fclose(pf);
	pf = NULL;
	return 0;
}


#include <errno.h>
#include <string.h>

int main()
{
	FILE* pf = fopen("test.txt", "w");
	if(pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 0;
	}
	//写文件
	fputc('a', pf);
	fputc('b', pf);
	fputc('c', pf);

	fclose(pf);
	pf = NULL;

	return 0;
}


int main()
{
	FILE* pf = fopen("test.txt", "r");
	int ch = 0;
	if(pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 0;
	}
	//读文件
	ch = fgetc(pf);
	printf("%c\n", ch);
	ch = fgetc(pf);
	printf("%c\n", ch);
	ch = fgetc(pf);
	printf("%c\n", ch);
	fclose(pf);
	pf = NULL;

	return 0;
}

int main()
{
	FILE* pf = fopen("test.txt", "r");
	int ch = 0;
	char buf[20] = {0};
	if(pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 0;
	}
	//读文件
	fgets(buf, 20, pf);
	printf("%s", buf);
	fgets(buf, 20, pf);
	printf("%s", buf);
	fclose(pf);
	pf = NULL;

	return 0;
}




int main()
{
	FILE* pf = fopen("test.txt", "w");
	int ch = 0;
	char buf[20] = {0};
	if(pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 0;
	}
	//写文件
	fputs("hello bit\n", pf);
	fputs("hello bit\n", pf);


	fclose(pf);
	pf = NULL;

	return 0;
}




struct S
{
	char name[20];
	int age;
	float score;
};

int main()
{
	struct S s = {0};

	FILE* pf = fopen("test.txt", "r");
	if(pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 0;
	}
	//读文件
	fscanf(pf, "%s %d %f", s.name, &(s.age), &(s.score));
	printf("%s %d %f\n", s.name, s.age, s.score);

	fclose(pf);
	pf = NULL;

	return 0;
}



int main()
{
	struct S s = {"zhou", 20, 59.5f};

	FILE* pf = fopen("test.txt", "w");
	int ch = 0;
	char buf[20] = {0};
	if(pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 0;
	}
	//写文件
	fprintf(pf, "%s %d %f", s.name, s.age, s.score);

	fclose(pf);
	pf = NULL;

	return 0;
}




struct S
{
	char name[20];
	int age;
	float score;
};

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


int main()
{
	struct S s = {"zhou", 20, 59.5f};

	FILE* pf = fopen("test.txt", "wb");
	if(pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 0;
	}
	//写文件
	fwrite(&s, sizeof(s), 1, pf);

	fclose(pf);
	pf = NULL;

	return 0;
}

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


int main()
{
	struct S s = {0};

	FILE* pf = fopen("test.txt", "rb");
	if(pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 0;
	}
	//二进制读文件
	fread(&s, sizeof(s), 1, pf);
	printf("%s %d %f\n", s.name, s.age, s.score);

	fclose(pf);
	pf = NULL;

	return 0;
}

stdin stdout stderr FILE*

int main()
{
	struct S s = {0};
	fscanf(stdin, "%s %d %f", s.name, &(s.age), &(s.score));
	//fprintf(stdout, "%s %d %f\n", "周", 20, 59.5f);
	fprintf(stdout, "%s %d %f\n", s.name, s.age, s.);
	return 0;
}

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


int main()
{
	struct S s = {"周", 20, 59.5f};
	struct S tmp = {0};

	char buf[30] = {0};

	//把结构体转换成字符串
	sprintf(buf, "%s %d %f", s.name, s.age, s.score);

	printf("字符串: %s\n", buf);
	sscanf(buf, "%s %d %f", tmp.name, &(tmp.age), &(tmp.score));
	printf("结构体: %s %d %f\n", tmp.name, tmp.age, tmp.score);
	return 0;
}

int main()
{

	FILE* pf = fopen("test.txt", "r");
	int ch = 0;
	if(pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 0;
	}
	//读文件
	ch = fgetc(pf);
	printf("%c\n", ch);
	fseek(pf, -3, SEEK_END);
	ch = fgetc(pf);
	printf("%c\n", ch);

	printf("%d\n", ftell(pf));//
	fclose(pf);
	pf = NULL;

	return 0;
}

int main()
{
	FILE* pf = fopen("test.txt", "r");
	int ch = 0;
	if (pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 0;
	}
	while ((ch = fgetc(pf)) != EOF)
	{
		printf("%c\n", ch);
	}
	//feof
	fclose(pf);
	pf = NULL;
	return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值