当前位置:网站首页>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;
}
边栏推荐
- 2. Const pointer
- info [email protected] : The platform “win32“ is incompatible with this module.
- LeetCode_字符串_简单_412.Fizz Buzz
- fatal: unsafe repository is owned by someone else 的解决方法
- Stm32-dac Experiment & high frequency DAC output test
- Tip: SQL Server blocked the state 'openrowset/opendatasource' of component 'ad hoc distributed queries'
- uniapp自动化测试学习
- Pychart connects to the remote server
- 数据库连接池和数据源
- Fabric.js 元素被选中时保持原有层级
猜你喜欢
The use of TestNG, the testing framework (II): the use of TestNG XML
为什么只会编程的程序员无法成为优秀的开发者?
JMeter script parameterization
大顶堆、小顶堆与堆排序
< 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
【NOI模拟赛】刮痧(动态规划)
Thoroughly master prototype__ proto__、 Relationship before constructor (JS prototype, prototype chain)
Stm32-dac Experiment & high frequency DAC output test
A white hole formed by antineutrons produced by particle accelerators
微信小程序使用towxml显示公式
随机推荐
一张图彻底掌握prototype、__proto__、constructor之前的关系(JS原型、原型链)
NLA自然语言分析实现数据分析零门槛
What is erdma? Popular science cartoon illustration
obsidian安装第三方插件——无法加载插件
STM32-DAC实验&高频DAC输出测试
threejs的控制器 立方體空間 基本控制器+慣性控制+飛行控制
taobao. trades. sold. Get query the transaction data that the seller has sold (according to the creation time), Taobao store sales order query API interface, Taobao R2 interface, Taobao oauth2.0 trans
STM32 library function for GPIO initialization
It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
Fabric. Usage of JS eraser (including recovery function)
Fabric. JS upper dash, middle dash (strikethrough), underline
Chapter 9: xshell free version installation
taobao. trade. memo. Add (add remarks to a transaction) interface, Taobao store flag insertion interface, Taobao order flag insertion API interface, oauth2.0 interface
4. Array pointer and pointer array
Ad20 cannot select the solution of component packaging in PCB editor
MQ教程 | Exchange(交换机)
##51单片机实验之简易验证码发生器
JMeter script parameterization
用户隐私协议有些汉字编码不规范导致网页显示乱码,需要统一找出来处理一下
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 '