当前位置:网站首页>c语言fprintf、fscanf、sscanf以及sprintf函数知识要点总结
c语言fprintf、fscanf、sscanf以及sprintf函数知识要点总结
2022-08-11 04:36:00 【BSP初级小学僧】
fprintf:
fprintf()和printf()一样工作。printf是打印输出到终端屏幕,fprintf是打印输出到文件。
fprintf()的返回值是输出的字符数,发生错误时返回一个负值.。
fprintf(fp,"arr[%d]=%d\n",i,arr[i]);//将写入的结果打印到指定的文件中进行输出
void fprintf_tset(void)
{
FILE *fp;
fp=fopen("fprintf_tset.txt","w");//打开文件方式为写入
int arr[]={1,2,3,4,5};
for(int i=0;i<5;i++)
{
fprintf(fp,"arr[%d]=%d\n",i,arr[i]);//将写入的结果打印到指定的文件中进行输出
}
fclose(fp);//关闭文件
}sprintf:
sprintf 是个变参函数,定义如下:
int sprintf( char *buffer, const char *format [, argument] ... );函数功能:把格式化的数据写入某个字符串
函数原型:int sprintf( char *buffer, const char *format [, argument] … );
返回值:字符串长度(strlen)
void sprintf_tset(void)
{
char str[100];
char str01[10]="135245";
sprintf(str,"%s",str01);
printf("%s",str);
}例如上面的一段代码,在str01的数组中存入一段格式化的字符串,再用sprintf函数去赋值写入str字符串,然后打印即可。
fscanf:
fscanf()函数的头文件是<stdio.h>,函数原型 为 int fscanf(FILE*stream, constchar*format, [argument...]);
其功能为根据数据格式(format)从输入流(stream)中写入数据(argument)。
【参数】stream为文件指针,format为格式化字符串,argument 为格式化控制符对应的参数。
从文件指针fp指向的文件中,按format中对应的控制格式读取数据,并存储在agars对应的变量中。
void fscanf_tset(void)
{
FILE *fp;
fp=fopen("fscanf_tset.txt","r");//打开文件方式为读取
int arr[10];//定义一个字符型数组去存
if(NULL==fp)
{
printf("无此文件!");//判断
}
else
{
fscanf(fp,"%d ",&arr[0]);//赋值于字符型数组中的元素值
}
printf("%d",arr[0]);//在终端中打印输出
fclose(fp);//关闭文件
}sscanf:
sscanf的作用:从一个字符串中读进于指定格式相符的数据。利用它可以从字符串中取出整数、浮点数和字符串。
sscanf和scanf的区别:scanf是以键盘作为输入源,sscanf是以字符串作为输入源。
void sscanf_tset(void)
{
char str[100];
char str01[10]="1352465";
sscanf("12345","%s",str);
sscanf(str01,"%s",str);
printf("%s",str);
}作业练习:
从两个不同文件中读取两串字符,讲这两串字符合并,并按照字母顺序进行排序,写入到第三文件中。
#include <stdio.h>
#include <string.h>
void fprintf_r(void)
{
FILE *fp01;
char ch01[100];
char arr[100];
fp01 = fopen("fprintf_r_01.txt","r");
if(NULL==fp01)
{
printf("无此文件");
}
else
{
fgets(ch01,40,fp01);
}
printf("fprintf_r_01.txt文件内容为:%s\n",ch01);
FILE *fp02;
char ch02[100];
fp02 = fopen("fprintf_r_02.txt","r");
if(NULL==fp01)
{
printf("无此文件");
}
else
{
fgets(ch02,40,fp02);
}
printf("fprintf_r_01.txt文件内容为:%s\n",ch02);
char str[20];
sprintf(str,"%s%s",ch01,ch02);//核心:直接写入两个字符串 把格式化的数据写入某个字符串中
printf("两个文件合并之后内容为:%s\n",str);
char *p=str;
int i,j;
char temp;
for(i=0;i<strlen(str)-1;i++)//冒泡排序
{
for(j=0;j<strlen(str)-1-i;j++)
{
if(*(p+j)>*(p+j+1))
{
temp=*(p+j);
*(p+j)=*(p+j+1);
*(p+j+1)=temp;
}
}
}
printf("两个文件合并并排序之后内容为:%s",str);
FILE *fp03;
char arr03[100];
fp03 = fopen("fprintf_r_03.txt","w");
fputs(str,fp03);
fclose(fp03);
}
int main()
{
fprintf_r();
return 0;
} 要点分析:
本题的难点在于,在获取到两串字符之后,如何进行合并操作,这时我们巧妙地运用到了sprintf()函数:
核心:直接写入两个字符串 把格式化的数据写入某个字符串中
sprintf(str,"%s%s",ch01,ch02);
剩下的就比较简单了,再用一下写入“w”操作,写到指定的文件中即可。
边栏推荐
- Solve the problem of multi-thread calling sql stored procedure
- [Server installation Redis] Centos7 offline installation of redis
- How to learn machine learning?machine learning process
- 校园兼职平台项目反思
- leetCode刷题14天二叉树系列之《 110 平衡二叉树判断》
- Overview of the JVM garbage collection and mechanism
- Clang Code Model: Error: The clangbackend executable “X:/clangbackend.exe“ could not be started
- 我的LaTeX入门
- 这些云自动化测试工具值得拥有
- JwsManager service interface implementation class - the jni implementation
猜你喜欢

LeetCode刷题第10天字符串系列之《125回文串验证》

干货:服务器网卡组技术原理与实践

这些云自动化测试工具值得拥有

How to learn machine learning?machine learning process

北湖区燕泉街道开展“戴头盔·保安全”送头盔活动

The custom of the C language types -- -- -- -- -- - structure

2022新员工公司级安全教育基础培训(118页)

(转)JVM中那些区域会发生OOM?

视觉任务种常用的类别文件之一json文件

The principle, architecture, implementation, practice of "transfer" and "search", no need to be afraid of interviews
随机推荐
使用百度EasyDL实现森林火灾预警识别
LeetCode刷题第16天之《239滑动窗口最大值》
力扣——旋转数组的最小数字
[Note] Is the value of BatchSize the bigger the better?
直播平台开发,Flutter,Drawer侧滑
findViewById返回null的问题
每日一题-滑动窗口
Switch---Spanning Tree---Three-layer Architecture Summary
shell monitors gpu usage
关于pom.xml文件
What is ensemble learning in machine learning?
快速使用UE4制作”大场景游戏“
How to add icons to web pages?
Object Creation and Display Transformation
.NET 服务注册
set_new_handler(0)是什么意思?有什么用?
洛谷P1196 银河英雄传说
我的LaTeX入门
Word2021 中的图片保存后就变模糊了
洛谷P4061 大吉大利,晚上吃鸡