C语言文件操作相关函数

文件操作相关函数

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include<stdlib.h>

基本操作

int main()
{
	int a = 10000;
	FILE* pf = fopen("test.txt", "wb");
	fwrite(&a, 4, 1, pf);//二进制的形式写到文件中
	//     4个字节 1个数 写入
	fclose(pf);
	pf = NULL;
	return 0;
}

fputc 写文件


//FILE

//FILE *fopen( const char *filename, const char *mode );


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

	fclose(pf);
	pf = NULL;
	return 0;
}


fgetc 读文件

int main()
{
	FILE* pf = fopen("test.txt", "r");//虽然r只有一个字母(非字符串),但是还是需要双引号
	//int ch = 0 ;
	if(pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 0;
	}

	//读文件
	ch = fgetc(pf);//fgetc的返回值是int 但是输出还是用%c
	//putchar(ch);
	printf("%c\n", ch);

	fclose(pf);
	pf = NULL;
	return 0;
}

fputs(“hello”, pf);
写一串字符串


int main()
{
	FILE* pf = fopen("test.txt", "w");
	int ch = 0 ;
	if(pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 0;
	}

	fputs("hello world", pf);

	fclose(pf);
	pf = NULL;
	return 0;
}


fgets(arr, 20, pf);
读取数据 返回值还是int 打印还是%c

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

	fgets(arr, 20, pf);
	printf("%s", arr);
	fgets(arr, 20, pf);
	printf("%s", arr);
	fgets(arr, 20, pf);
	printf("%s", arr);
	fclose(pf);
	pf = NULL;
	return 0;
}


//scanf
//printf
//格式化的输入输出函数---标准输入/输出


//fscanf-->四个函数中,只有这个是读
//fprintf
//格式化的形式写入文件或者标准输出-fprintf
//格式化的形式从文件或者标准输入读取-fscanf



//sscanf
//sprintf

//把一个结构体变量的数据以格式化的形式转换为字符串存储
sprintf(buf, “%s %d”, s.name, s.age);
//将 s.name, s.age写入buf
//字符串转换为结构体[ buf 也是数组名]
fprintf/sprintf(buf, “%s %d”, s.name, s.age);–>(都是将后面参数(结构体)写到前面参数地址(文件指针)中),后面参数位置不需要取地址【重点】
sscanf(buf, “%s %d”, tmp.name , &(tmp.age));
//将buf对应写入tmp(结构体) 需要取地址,而tmp.name是数组名

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

//
//序列化和反序列化
//xml
//json-cjson
//

int main()
{
	struct S s = { "张三", 20 };
	char buf[20] = { 0 };
	struct S tmp = { 0 };
	sprintf(buf, "%s %d", s.name, s.age);
	sscanf(buf, "%s %d", tmp.name, &(tmp.age));
//sscanf/fscanf  --- 都是将前面的buf指针,写入或者读入后面的结构体
	printf("%s %d\n", tmp.name, tmp.age);
	system("pause");
	return 0;
}

//读 【 重点理解记忆 】
fscanf(pf, “%s %d”, s.name, &(s.age));
//读ps(文件指针)的内容到结构体s

struct S
{
	char name[20];
	int age;
};
//stdin -标准输入-键盘  FILE*
//stdout-标准输出-屏幕 FILE*
//stderr-标准错误-屏幕 FILE*

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", s.name, &(s.age));
	//fscanf(stdin, "%s %d", s.name, &(s.age));//ok

	//打印
	printf("%s %d\n", s.name, s.age);
	fclose(pf);
	pf = NULL;
	system("pause");
	return 0;
}

//写
fprintf(pf, “%s %d”, s.name, s.age);
//将结构体s里面的成员写入文件(文件指针pf)

int main()
{
	struct S s = {"zhangsan", 20};
	
	FILE* pf = fopen("test.txt","w");
	if(pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 0;
	}
	//写
	//fprintf(stdout, "%s %d", s.name, s.age);
	fprintf(pf, "%s %d", s.name, s.age);//将结构体s里面的成员写入文件(文件指针pf)

	fclose(pf);
	pf = NULL;
	return 0;
}
//sscanf/fscanf(pf, "%s %d", s.name, &(s.age));  --- 都是将前面的buf指针,写入或者读入后面的结构体(注意要取地址)
//sprintf/fprintf(pf, "%s %d", s.name, s.age); --- 都是讲后面的结构体成员,写入前面的文件指针



//写操作
fwrite(&s, sizeof(struct S), 1, pf);
//将s结构体的内容写到pf指向的文件中去,s要取地址。1代表1个结构体


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

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


int main()
{
	struct S s = {"张三", 20};

	FILE* pf = fopen("test.txt", "wb");
	if(pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 0;
	}
	//写操作
	fwrite(&s, sizeof(struct S), 1, pf);//将s结构体的内容写到pf指向的文件中去,s要取地址。1代表1个结构体
	fclose(pf);
	pf = NULL;
	return 0;
}

//读操作
fread(&s, sizeof(struct S), 1, pf);
//将pf指向的文件s读到结构体的内容中去,s要取地址

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(struct S), 1, pf);//将pf指向的文件s读到结构体的内容中去,s要取地址
	printf("%s %d\n", s.name, s.age);

	fclose(pf);
	pf = NULL;
	system("pause");
	return 0;
}

	//fwrite(&s, sizeof(struct S), 1, pf);//将s结构体的内容写到pf指向的文件中去,s要取地址。1代表1个结构体
	//fread(&s, sizeof(struct S), 1, pf);//将pf指向的文件s读到结构体的内容中去,s要取地址

fseek:根据文件指针的位置和偏移量来定位文件指针
ftell:返回文件指针相对于起始位置的偏移量
rewind:让文件指针的位置回到文件的起始位置

int main()
{
	FILE* pf = fopen("example.txt", "r");
	int ch = 0;
	if(pf == NULL)
		return 0;
	//目前文件中总字节数为17
	fseek(pf, -1, SEEK_END);//文件指针定位到 末尾 的 前面1个的位置(偏移量ftell为16)
	printf("%d\n", ftell(pf));//偏移量为16

	ch = fgetc(pf);//文件指针往后走了一个 (偏移量为17)
	putchar(ch);//打印一个字符
	printf("%d\n", ftell(pf));//偏移量为17

	fseek(pf, 3, SEEK_CUR);//文件指针从当前位置 向后 走3个位置,偏移量 17 + 3 = 20
	printf("%d\n", ftell(pf));//偏移量为20

	ch = fgetc(pf);//文件指针往后走了一个 (偏移量为21)其实是20/文件指针最大偏移量是20,不能再多了
	putchar(ch);//打印一个字符
	printf("%d\n", ftell(pf));//偏移量为21  其实是20

	fseek(pf, -9,SEEK_CUR);//文件指针从当前位置 向前 走3个位置,偏移量 20 - 9 = 11
	printf("%d\n", ftell(pf));//偏移量为11

	rewind(pf);//文件指针回到最开始,偏移量ftell(pf) = 0
	ch = fgetc(pf);//文件指针往前走了一个位置
	putchar(ch);
	printf("%d\n", ftell(pf));//偏移量为1

	fclose(pf);
	pf = NULL;
	system("pause");
	return 0;
}

fputs 写入时会覆盖他原来的数据


int main()
{
	FILE * pFile;
	pFile = fopen("example.txt", "wb");
	fputs("This is an apple.", pFile);
	fseek(pFile, 9, SEEK_SET);
	fputs(" sam", pFile);//This is a sample.
	fclose(pFile);
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值