当前位置:网站首页>Printf function and scanf function in C language
Printf function and scanf function in C language
2022-07-02 14:56:00 【theskylife】
1. scanf、sscanf and fscanf function
1.1 scanf function
Standard input functions . The grammar is as follows :
int scanf( const char *format, … )
By using %s Format control string completion , But you can't read in spaces 、 Line break 、 tabs (\t) So score separator . Can pass %[^%n]s Complete the reading of the string with spaces .
1.2 sscanf function
s representative string, Used to read formatted characters
// Examples of use
sscanf("1/2/3", "%d/%d/%d", &a1, &a2, &a3)
1.3 fscanf function
f representative file, Perform formatted input from a stream
char str_r[1000]={
0};
FILE *fp_r = fopen("./test.txt", "r");
fscanf(fp_r, "%[^\n]s", str_r);
fclose(fp_r);
printf("%s\n", str_r);
return 0;
2. printf、sprintf and fprintf function
2.1 prinrf function
Standard output function . The grammar is as follows :int printf(const char *format, …)
2.2 sprintf function
The function used to format the output string
sprintf("1/2/3","%d-%d-%d", a1, a2, a3)
2.3 fprintf function
Formatted string , And output the results to the formulated documents , Know that the end of string appears (\0") until
char str[1000]="Hello world!";
FILE *fp = fopen("./test.txt","w");
fprintf(fp, "%s", str);
fclose(fp);
3. Use small examples
3.1 sscanf and sprintf
#include<stdio.h>
int main(){
char str[100] = "192.168.0.1";
int a, b, c, d;
sscanf(str, "%d.%d.%d.%d", &a, &b, &c, &d);
printf("a = %d, b = %d, c = %d, d = %d\n", a, b, c, d);
sprintf(str, "%[email protected]%[email protected]%[email protected]%d\n", a, b, c, d);
int num = printf("str=%s",str);
printf("%d", num);
return 0;
}
3.2 fscanf and fprintf
#include<stdio.h>
int main(){
char str[1000]="Hello world!";
char str_r[1000]={
0};
FILE *fp = fopen("./output","w");
fprintf(fp, "%s", str);
fclose(fp);
FILE *fp_r = fopen("./output", "r");
fscanf(fp_r, "%[^\n]s", str_r);
fclose(fp_r);
printf("%s\n", str_r);
return 0;
}
边栏推荐
- MFC timer usage
- Some Chinese character codes in the user privacy agreement are not standardized, which leads to the display of garbled codes on the web page. It needs to be found and handled uniformly
- Reuse and distribution
- 3、函数指针和指针函数
- 实用调试技巧
- 华为面试题: 没有回文串
- Uniapp automated test learning
- 天猫商品详情接口(APP,H5端)
- fatal: unsafe repository is owned by someone else 的解决方法
- c语言入门--数组
猜你喜欢
c语言入门--数组
大顶堆、小顶堆与堆排序
##51单片机实验之简易验证码发生器
关于网页中的文本选择以及统计选中文本长度
Kityformula editor configure font size and spacing
buuctf-pwn write-ups (7)
Tip: SQL Server blocked the state 'openrowset/opendatasource' of component 'ad hoc distributed queries'
【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
C language exercises - (array)
[email protected]: The platform “win32“ is incompatible with this module."/>
info [email protected]: The platform “win32“ is incompatible with this module.
随机推荐
Simple verification code generator for 51 single chip microcomputer experiment
C#代码审计实战+前置知识
【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
MFC CString to char*
Arithmetic operations and related exercises in C language
String matching problem
用户隐私协议有些汉字编码不规范导致网页显示乱码,需要统一找出来处理一下
btrace-(字节码)动态跟踪工具
Database connection pool and data source
buuctf-pwn write-ups (7)
LeetCode - 搜索二维矩阵
天猫商品详情接口(APP,H5端)
为什么只会编程的程序员无法成为优秀的开发者?
2. Const pointer
taobao. logistics. dummy. Send (no logistics delivery processing) interface, Taobao store delivery API interface, Taobao order delivery interface, Taobao R2 interface, Taobao oau2.0 interface
Large top heap, small top heap and heap sequencing
Thoroughly master prototype__ proto__、 Relationship before constructor (JS prototype, prototype chain)
Fatal: unsafe repository is owned by someone else
Yolov6 training: various problems encountered in training your dataset
LeetCode 209. 长度最小的子数组