当前位置:网站首页>C语言中的printf函数和scanf函数
C语言中的printf函数和scanf函数
2022-07-02 11:38:00 【theskylife】
1. scanf、sscanf和fscanf函数
1.1 scanf函数
标准输入函数。语法如下:
int scanf( const char *format, … )
通过使用%s格式控制字符串完成,但无法读入空格、换行、制表符(\t)这样得分隔符。可以通过%[^%n]s完成带空格的字符串的读入。
1.2 sscanf函数
s代表string,用来读取格式化的字符
//使用例子
sscanf("1/2/3", "%d/%d/%d", &a1, &a2, &a3)
1.3 fscanf函数
f代表file,从一个流中执行格式化输入
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和fprintf函数
2.1 prinrf函数
标准输出函数。语法如下:int printf(const char *format, …)
2.2 sprintf函数
用来格式化输出字符串的函数
sprintf("1/2/3","%d-%d-%d", a1, a2, a3)
2.3 fprintf函数
格式化字符串,并将结果输出到制定的文件中,知道出现字符串结束(\0")为止
char str[1000]="Hello world!";
FILE *fp = fopen("./test.txt","w");
fprintf(fp, "%s", str);
fclose(fp);
3.使用小例子
3.1 sscanf和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和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;
}
边栏推荐
- Daily learning 3
- Fabric. Usage of JS eraser (including recovery function)
- C code audit practice + pre knowledge
- STM32标准固件库函数名记忆(二)
- mathjax 入门(web显示数学公式,矢量的)
- There is no solution to the decryption error of the remote user 'sa' and the service master password mapped from the remote server 'to the local user' (null) /sa '
- C语言高级用法--函数指针:回调函数;转换表
- fatal: unsafe repository is owned by someone else 的解决方法
- 数据库连接池和数据源
- OpenCV调用USB摄像头的点滴
猜你喜欢

复用和分用

< schematic diagram of oral arithmetic exercise machine program development> oral arithmetic exercise machine / oral arithmetic treasure / children's math treasure / children's calculator LCD LCD driv

Certik released the defi security report in 2021, disclosing key data of industry development (PDF download link attached)

Tujia muniao meituan has a discount match in summer. Will it be fragrant if the threshold is low?

Fabric.js 自由绘制圆形
![[Space & single cellomics] phase 1: single cell binding space transcriptome research PDAC tumor microenvironment](/img/e1/c8e81570ab78de1e488a611c25ebb9.png)
[Space & single cellomics] phase 1: single cell binding space transcriptome research PDAC tumor microenvironment

Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.

Factal: Unsafe repository is owned by someone else Solution

There is no solution to the decryption error of the remote user 'sa' and the service master password mapped from the remote server 'to the local user' (null) /sa '

JMeter script parameterization
随机推荐
ONNX+TensorRT:将预处理操作写入ONNX并完成TRT部署
[QNX hypervisor 2.2 user manual]6.3 communication between guest and external
There is no solution to the decryption error of the remote user 'sa' and the service master password mapped from the remote server 'to the local user' (null) /sa '
【NOI模拟赛】伊莉斯elis(贪心,模拟)
Large top heap, small top heap and heap sequencing
LeetCode_字符串_简单_412.Fizz Buzz
obsidian安装第三方插件——无法加载插件
【无标题】LeetCode 2321. 拼接数组的最大分数
NLA natural language analysis realizes zero threshold of data analysis
华为面试题: 没有回文串
[Space & single cellomics] phase 1: single cell binding space transcriptome research PDAC tumor microenvironment
taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
由粒子加速器产生的反中子形成的白洞
Fabric.js 橡皮擦的用法(包含恢复功能)
C语言高级用法--函数指针:回调函数;转换表
提示:SQL Server 阻止了对组件‘Ad Hoc Distributed Queries ‘的STATEMENT ‘OpenRowset/OpenDatasource“”
电脑怎么设置扬声器播放麦克风的声音
Makefile separates file names and suffixes
JMeter script parameterization
Advanced usage of C language -- function pointer: callback function; Conversion table