文件字符的读和写

文件字符的写

将一个字符写入一个文本(.txt)中去

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<errno.h>
int main()
{
	//打开文件
	FILE* pf = fopen("test.txt", "w");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	//写文件
	char i = 0;
	for (i = 'a'; i <= 'z'; i++)
	{
		fputc(i, pf);
	}

	//关闭文件
	fclose(pf);
	pf = NULL;
	return 0;
}

输出结果:


文件字符的读

将文本中的数据,读到屏幕上

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<errno.h>
int main()
{
	//打开文件
	FILE* pf = fopen("test.txt", "r");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	//读取文件
	int i = 0;
	char ch = 0;
	ch = fgetc(pf);
	printf("%c\n", ch);
	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;
}

输出的结果:


stdin-标准输入流,在大多数环境中从键盘输入,scanf函数就是从标准输入流中读取数据。

stdout-标准输出流,大多数的环境中输出至显示器界面,printf函数就是将信息输出到标准输出流中。

stderr-标准错误流,大多数环境中输出到显示器界面。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
	//输入
	char ch ;
	scanf("%c", &ch);
	//输出
	printf("%c\n", ch);
	return 0;
}

这个代码类似于 

#include<stdio.h>
int main()
{
	//输入
	int ch = fgetc(stdin);
	//输出
	fputc(ch, stdout);
	return 0;
}

输出结果:


 fputs的应用

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<errno.h>
int main()
{
	FILE* pf = fopen("test.txt", "w");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	fputs("I am a student.\n", pf);
	fputs("Are you ok???\n", pf);
	fclose(pf);
	pf = NULL;
	return 0;
}

输出结果:

是在test.txt文件中,写出


fgets的应用

1.读十个字符,其实只读了9个字符,最后一个位置放置\0

2.遇见\n也会停止读取

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<errno.h>
int main()
{
	FILE* pf = fopen("test.txt", "r");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	char a[20] ="xxxxxxxxxxxxxxxxxx";
	fgets(a, 10, pf);
	printf("%s\n", a);
	fclose(pf);
	pf = NULL;
	return 0;
}

 输出结果:


 同样的,stdin和stdout一样的可以用来表示输出输入

 stdin-标准输入流,在大多数环境中从键盘输入,scanf函数就是从标准输入流中读取数据。

stdout-标准输出流,大多数的环境中输出至显示器界面,printf函数就是将信息输出到标准输出流中。

stderr-标准错误流,大多数环境中输出到显示器界面。

#include<stdio.h>
int main()
{
	char a[20] = { 0 };
	fgets(a, 10, stdin);
	fputs(a, stdout);
	return 0;
}

输出结果:


fprintf的应用

fprintf和printf的区别,就是多一个FILE* stream

printf

int printf(const char* formation,...) 默认输出流stdout

fprintf

int fprintf(FILE* stream,const char* formation,...)

//fprintf的应用
//
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<errno.h>
int main()
{
	char name[20] = "zhangsan";
	int age = 20;
	float score = 95.5f;
	FILE* pf = fopen("test.txt", "w");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	fprintf(pf, "%s\n %d\n %.2f\n", name, age, score);
	fclose(pf);
	pf = NULL;
	return 0;
}

用结构体来传输数据:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<errno.h>
struct S
{
	char name[20] ;
	int age;
	float score ;
};
int main()
{
	struct S s = { "zhangsan",20,95.5 };
	FILE* pf = fopen("test.txt", "w");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	fprintf(pf, "%s\n %d\n %.2f\n", s.name, s.age, s.score);
	fclose(pf);
	pf = NULL;
	return 0;
}

 输出结果:


fscanf的应用

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<errno.h>
struct S
{
	char name[20] ;
	int age;
	float score ;
};
int main()
{
	struct S s = { 0 };
	FILE* pf = fopen("test.txt", "r");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	//从文件中读取信息,存放到s的各个成员中去
	fscanf(pf, "%s %d %f", s.name, &s.age, &s.score);
	//打印在屏幕上
	printf("%s %d %.2f", s.name, s.age, s.score);
	fclose(pf);
	pf = NULL;
	return 0;
}

输出结果:


sprintf的运用

是将格式化的数据,转化成字符串

sprintf

int sprintf(char* str,const char* format,...)

//sprintf的应用
//
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<errno.h>
struct S
{
	char name[20];
	int age;
	float score;
};
int main()
{
	struct S s = { "zhangsan",20,89.7};
	FILE* pf = fopen("test.txt", "w");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	char arr[100] = { 0 };
	sprintf(arr, "%s %d %.2f", s.name, s.age, s.score);
	printf("%s\n", arr);
	fclose(pf);
	pf = NULL;
	return 0;
}

 输出结果:


sscanf的应用 

在字符串中,提取格式化的数据

sscanf

int sscanf(const char* s,const char* format,...)

 

//sscanf的应用
//
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<errno.h>
struct S
{
	char name[20];
	int age;
	float score;
};
int main()
{
	struct S s = { "zhangsan",20,89.7};
	FILE* pf = fopen("test.txt", "w");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	char arr[100] = { 0 };
	//将s中的各个数据转换成字符串,存放在arr中
	sprintf(arr, "%s %d %.2f", s.name, s.age, s.score);
	//printf("%s\n", arr);
	//从字符串arr中,提取格式化的数据,存放在tmp中
	struct S tmp = { 0 };
	sscanf(arr, "%s %d %f", tmp.name, &tmp.age, &tmp.score);
	printf("%s %d %.2f\n", tmp.name, tmp.age, tmp.score);
	fclose(pf);
	pf = NULL;
	return 0;
}

输出结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值