当前位置:网站首页>Summary of c language fprintf, fscanf, sscanf and sprintf function knowledge points
Summary of c language fprintf, fscanf, sscanf and sprintf function knowledge points
2022-08-11 04:42:00 【BSP Junior Primary School Monk】
fprintf:
fprintf() works the same as printf().printf prints output to the terminal screen, fprintf prints output to a file.
The return value of fprintf() is the number of characters output, or a negative value if an error occurs.
fprintf(fp,"arr[%d]=%d\n",i,arr[i]);//Print the written result tooutput in the specified file
void fprintf_tset(void){FILE *fp;fp=fopen("fprintf_tset.txt","w");//Open the file by writingint arr[]={1,2,3,4,5};for(int i=0;i<5;i++){fprintf(fp,"arr[%d]=%d\n",i,arr[i]);//Print the written result to the specified file for output}fclose(fp);//Close the file}
sprintf:
sprintf is a variadic function defined as follows:
int sprintf( char *buffer, const char *format [, argument] ... );
Function function: write formatted data to a string
Function prototype: int sprintf( char *buffer, const char *format [, argument] … );
Return value: string length(strlen)
void sprintf_tset(void){char str[100];char str01[10]="135245";sprintf(str,"%s",str01);printf("%s",str);}
For example, in the above piece of code, store a formatted string in the array of str01, and then use the sprintf function to assign and write the str string, and then print it.
fscanf:
The header file of the fscanf() function is
Its function is to write data (argument) from the input stream (stream) according to the data format (format).
[Parameter] stream is the file pointer, format is the format string, and argument is the parameter corresponding to the format control character.
From the file pointed to by the file pointer fp, the data is read in the corresponding control format in format and stored in the variable corresponding to agars.
void fscanf_tset(void){FILE *fp;fp=fopen("fscanf_tset.txt","r");//The way to open the file is to readint arr[10];//Define a character array to storeif(NULL==fp){printf("No such file!");//Judgment}else{fscanf(fp,"%d ",&arr[0]);//The element value assigned to the character array}printf("%d",arr[0]);//Print the output in the terminalfclose(fp);//Close the file}
sscanf:
The role of sscanf: from a String reads data that matches the specified format.It can be used to extract integers, floating-point numbers and strings from strings.
The difference between sscanf and scanf: scanf takes the keyboard as the input source, and sscanf takes the string as the input source.
void sscanf_tset(void){char str[100];char str01[10]="1352465";sscanf("12345","%s",str);sscanf(str01,"%s",str);printf("%s",str);}
Homework exercise:
Read two strings of characters from two different files, merge the two strings, sort them alphabetically, and write them into a third file.
#include #include void fprintf_r(void){FILE *fp01;char ch01[100];char arr[100];fp01 = fopen("fprintf_r_01.txt","r");if(NULL==fp01){printf("No such file");}else{fgets(ch01,40,fp01);}printf("fprintf_r_01.txt file content is: %s\n",ch01);FILE *fp02;char ch02[100];fp02 = fopen("fprintf_r_02.txt","r");if(NULL==fp01){printf("No such file");}else{fgets(ch02,40,fp02);}printf("fprintf_r_01.txt file content is: %s\n",ch02);char str[20];sprintf(str,"%s%s",ch01,ch02);//Core: write two strings directly, write formatted data into a stringprintf("The content of the merged two files is: %s\n",str);char *p=str;int i,j;char temp;for(i=0;i*(p+j+1)){temp=*(p+j);*(p+j)=*(p+j+1);*(p+j+1)=temp;}}}printf("The contents of the two files after merging and sorting are: %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;}
Analysis of key points:
The difficulty of this question is how to merge after obtaining two strings of characters. At this time, we cleverly used the sprintf() function:
Core: Write two strings directly to write formatted data into a string
sprintf(str,"%s%s",ch01,ch02);span>
The rest is relatively simple, and then use the write "w" operation to write to the specified file.
边栏推荐
- Mysql:设置主键自动增长起始值
- Clang Code Model: Error: The clangbackend executable “X:/clangbackend.exe“ could not be started
- 北湖区燕泉街道开展“戴头盔·保安全”送头盔活动
- [Server installation Redis] Centos7 offline installation of redis
- 直播软件搭建,流式布局,支持单选、多选等
- 使用百度EasyDL实现施工人员安全装备检测
- "125 Palindrome Verification" of the 10th day string series of LeetCode brushing questions
- Three 】 【 yolov7 series of actual combat from 0 to build training data sets
- 增加PRODUCT_BOOT_JARS及类 提供jar包给应用
- 洛谷P4560 Wall 砖墙
猜你喜欢
交换机--- 生成树--三层架构总结
【yolov7系列三】实战从0构建训练自己的数据集
【FPGA】abbreviation
Use jackson to parse json data in detail
[Likou] 22. Bracket generation
简历里写了会代码,却依然过不了面试这一关
Three 】 【 yolov7 series of actual combat from 0 to build training data sets
Word2021 中的图片保存后就变模糊了
To break the bottleneck of transactional work, the gentleman signs the electronic contract to release the "source power" of HR!
Apache初体验
随机推荐
What is machine learning?Explain machine learning concepts in detail
send_sig: kernel execution flow
.NET Custom Middleware
这些云自动化测试工具值得拥有
交换机--- 生成树--三层架构总结
C# 一周入门高级编程之《C#-LINQ》Day Four
Word2021 中的图片保存后就变模糊了
[Server installation Redis] Centos7 offline installation of redis
力扣——青蛙跳台阶问题
WPF DataGrid 使用数据模板(2)
二叉堆的基础~
源代码加密技术浅析
How to learn machine learning?machine learning process
洛谷P4324 扭动的回文串
"125 Palindrome Verification" of the 10th day string series of LeetCode brushing questions
Get Qt installation information: including installation directory and various macro addresses
关于pom.xml文件
【FPGA教程案例49】控制案例1——基于FPGA的PID控制器verilog实现
利用Navicat Premium导出数据库表结构信息至Excel
Listen to pull out U disk inserted into the message, U disk drive