当前位置:网站首页>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;
}
边栏推荐
- tmall.product.schema.get( 产品信息获取schema获取 ),淘宝店铺上传商品API接口,淘宝商品发布接口,淘宝商品上传API接口,店铺上传接口,oAuth2.0接口
- < schéma de développement de la machine d'exercice oral > machine d'exercice oral / trésor d'exercice oral / trésor de mathématiques pour enfants / lecteur LCD de calculatrice pour enfants IC - vk1621
- buuctf-pwn write-ups (7)
- buuctf-pwn write-ups (7)
- Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
- Reuse and distribution
- 删除元素(带过渡动画)
- Fabric.js 上划线、中划线(删除线)、下划线
- Fabric. JS upper dash, middle dash (strikethrough), underline
- 用户隐私协议有些汉字编码不规范导致网页显示乱码,需要统一找出来处理一下
猜你喜欢
天猫商品详情接口(APP,H5端)
fatal: unsafe repository is owned by someone else 的解决方法
Fabric.js 上划线、中划线(删除线)、下划线
##51单片机实验之简易验证码发生器
电脑怎么设置扬声器播放麦克风的声音
微信小程序使用towxml显示公式
A white hole formed by antineutrons produced by particle accelerators
Reuse and distribution
Li Chuang EDA learning notes 15: draw border or import border (DXF file)
YoloV6训练:训练自己数据集遇到的各种问题
随机推荐
用户隐私协议有些汉字编码不规范导致网页显示乱码,需要统一找出来处理一下
Actual combat sharing of shutter screen acquisition
删除元素(带过渡动画)
Tmall product details interface (APP, H5 end)
NLA natural language analysis realizes zero threshold of data analysis
STM32标准固件库函数名记忆(二)
Fabric. Usage of JS eraser (including recovery function)
Tip: SQL Server blocked the state 'openrowset/opendatasource' of component 'ad hoc distributed queries'
qml 弹窗框架,可定制
taobao. trade. Get (get some information of a single transaction), Taobao store order interface, Taobao oauth2.0 interface, Taobao R2 interface code docking and sharing
tmall. product. schema. Get (product information acquisition schema acquisition), Taobao store upload commodity API interface, Taobao commodity publishing interface, Taobao commodity upload API interf
提示:SQL Server 阻止了对组件‘Ad Hoc Distributed Queries ‘的STATEMENT ‘OpenRowset/OpenDatasource“”
Fabric.js 橡皮擦的用法(包含恢复功能)
PTA题库 ===>复数四则运算,一帮一,考试座位号(7-73)
字符串匹配问题
It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
Fabric. JS free drawing ellipse
Bit by bit of OpenCV calling USB camera
大顶堆、小顶堆与堆排序
Fabric.js 元素被选中时保持原有层级