文件操作命令以及scanf fscanf sscanf printf fprintf sprintf day19

scanf: 从标准输入流  读取格式化的数据、

fscanf: 从所有的输入流 读取格式化的数据

sscanf:从字符串中读取一个格式化的数据或者 把一个字符串转成一个格式化的数据

printf:把格式化的数据  输出到标准输出上

fprintf:把格式化的数据  输出到所有输出流上

sprintf:把格式化的数据转化成对应的字符串:

fputc 将字符a的内容放到data.txt文件中

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
int main() {

	FILE* ps = fopen("data.txt", "w");
	char a = 'A';
	for (a = 'A'; a <= 'Z'; a++)
	{
		fputc(a, ps);
	}
	fclose(ps);
	ps = NULL; 
	
	system("pause");
	return 0;
}

fgetc 将刚才放入的内容读出来;fgetc 得到的是ascii

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

int main() {

	FILE* ps = fopen("data.txt", "r");
	int ch;
	ch = fgetc(ps);
	printf("%c\n", ch);
	ch = fgetc(ps);
	printf("%c\n", ch);
	ch = fgetc(ps);
	printf("%c\n", ch);
	ch = fgetc(ps);
	printf("%c\n", ch);
	ch = fgetc(ps);
	printf("%c\n", ch);

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

fputs   将写入的字符串放入文件中 将pc的内容 写入到 ps中去

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

int main() {

	FILE* ps = fopen("data.txt", "w");
	char pc[] = "dsfds";
	fputs(pc, ps);
	
	fclose(ps);
	ps = NULL; 
	
	system("pause");
	return 0;
}

fgets  将ps的内容在读出来

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

int main() {

	FILE* ps = fopen("data.txt", "r");
	char pc[20] = {0};//这个地方不能这样charpc[]={0};这样的话会跳出stack corrupt
	fgets(pc,5,ps);  //这个地方如果写入5 那么只能读取四个字符 因为最后他要加入一个'\0';
	printf("%s\n", pc);
	fclose(ps);
	ps = NULL; 
	
	system("pause");
	return 0;
}

sprintf   将信息输入到文件中 

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

struct stu {

	int a;
	char b;

};
int main() {

	struct stu s = { 8,'c' };
	FILE* ps = fopen("data.txt", "w");

	fprintf(ps, "%d %c", s.a, s.b);  //简单理解为就是之前我们printf都是打印到屏幕 现在打印到了文件


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

sscanf  将文件中的内容读出来

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

struct stu {

	int a;
	char b;

};
int main() {

	struct stu s = {0};
	FILE* ps = fopen("data.txt", "r");

	fscanf(ps, "%d %c", &(s.a), &(s.b));//都给谁后面就写谁的地址
	printf("%d %c", s.a, s.b);

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

二进制的读写 fwrite fread

fwrite

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
struct stu {

	int a;
	int b;
	char name[10];

};
int main() {

	struct stu s = {1,1,"dada"};
	FILE* ps = fopen("data.txt", "wb");
    if(ps==NULL)
{   
       strerror(errno);
}

	fwrite(&s, sizeof(struct stu), 1, ps);

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

fread

  

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

struct stu {

	int a;
	int b;
	char name[10];

};
int main() {

	struct stu s = {1,1,"dada"};
	FILE* ps = fopen("data.txt", "rb");


	fread(&s, sizeof(struct stu), 1, ps);
	printf("%d %d %s\n", s.a, s.b, s.name);
	
	fclose(ps);
	ps = NULL; 
	
	system("pause");
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值