- scanf(): C库函数int scanf(const char*format,…)从stdin读取格式化输入。
Syntax: int scanf(const char *format, ...) Return type: Integer Parameters: format: string that contains the type specifier(s) "..." (ellipsis): indicates that the function accepts a variable number of arguments
每个参数必须是一个将转换结果写入的内存地址。成功后,函数返回已填充的变量数。如果输入失败,在成功读取任何数据之前,将返回EOF。 可在scanf中使用的类型说明符:
%c — Character %d — Signed integer %f — Floating point %s — String
//C program t illustrate scanf statement
#include <stdio.h>
#include <stdlib.h>
int
main()
{
char
a[10];
printf
(
"Please enter your name : "
);
//scanf statement
scanf
(
"%s"
, a);
printf
(
"You entered: %s"
, a);
return
0;
}
输入:
Geek
输出:
Please enter your name : You entered: Geek
- sscanf(): sscanf()用于从字符串中读取格式化输入。
Syntax: int sscanf ( const char * s, const char * format, ...); Return type: Integer Parameters: s: string used to retrieve data format: string that contains the type specifier(s) … : arguments contains pointers to allocate storage with appropriate type. There should be at least as many of these arguments as the number of values stored by the format specifiers.
成功后,函数返回已填充的变量数。在输入失败的情况下,在成功读取任何数据之前,会返回EOF。
// C program to illustrate sscanf statement
#include <stdio.h>
int
main ()
{
// declaring array s
char
s [] =
"3 red balls 2 blue balls"
;
char
str [10],str2 [10];
int
i;
// %*s is used to skip a word
sscanf
(s,
"%d %*s %*s %*s %s %s"
, &i, str, str2);
printf
(
"%d %s %s "
, i, str, str2);
return
0;
}
输出:
3 blue balls
- fscanf() :fscanf()从文件中读取格式化数据并将其存储到变量中。
Syntax: int fscanf(FILE *stream, const char *format, ...) Parameters: Stream: pointer to the File object that identifies the stream. format : is a string that contains the type specifier(s)
成功后,函数返回已填充的变量数。在输入失败的情况下,在成功读取任何数据之前,会返回EOF。
// C program to illustrate sscanf statement
// This program will run on system having the file file.txt
#include <stdio.h>
#include <stdlib.h>
int
main()
{
char
s1[10], s2[10], s3[10];
int
year;
// file pointer
FILE
* fp;
// opening/creation of file
fp =
fopen
(
"file.txt"
,
"w+"
);
// storing string in the file
fputs
(
"Hello World its 2017"
, fp);
// sets the file position to the beginning of the file
rewind
(fp);
// taking input from file
fscanf
(fp,
"%s %s %s %d"
, s1, s2, s3, &year);
printf
(
"String1 |%s|"
, s1 );
printf
(
"String2 |%s|"
, s2 );
printf
(
"String3 |%s|"
, s3 );
printf
(
"Integer |%d|"
, year );
// close file pointer
fclose
(fp);
return
(0);
}
输出:
String1 |Hello| String2 |World| String3 |its| Integer |2017|
- scanf_s(): 此功能特定于Microsoft编译器。它与scanf相同,只是不会导致缓冲区过载。
Syntax: int scanf_s(const char *format [argument]...); argument(parameter): here you can specify the buffer size and actually control the limit of the input so you don't crash the whole application.
成功后,函数返回已填充的变量数。在输入失败的情况下,在成功读取任何数据之前,会返回EOF。 为什么要使用scanf_s()? scanf只读取控制台提供的任何输入。C不会检查用户输入是否适合指定的变量。 如果您有一个名为color[3]的数组,并使用scanf表示“Red”,它将正常工作,但如果用户输入的字符超过3个,scanf将开始写入不属于color的内存。C不会捕捉到这一点或警告您,它可能会或可能不会使程序崩溃,这取决于是否有东西试图访问和写入不属于color的内存插槽。这就是scanf_s发挥作用的地方。scanf_s检查用户输入是否适合给定的内存空间。
// C program to illustrate sscanf_s statement
// scanf_s() will only work in Microsoft Visual Studio.
#include <stdio.h>
#include <stdlib.h>
int
main()
{
char
a[5];
// sizeof(a) is buffer size
scanf_s(
"%s"
, a,
sizeof
(a));
printf
(
"%s "
, a);
return
0;
}
输入:
Red
输出:
Red
输入:
Yellow
输出:
No Output
说明缓冲区大小和数组大小之间的关系。
C
// C program
// consumes the Enter key
// (newline character) pressed after input
#include<stdio.h>
char
ch[100000];
printf
(
"Enter characters: "
);
scanf_s(
"%s"
, ch, 99999);
getchar
();
C++
// C++ program
// consumes the Enter key
// (newline character) pressed after input
#include "stdafx.h"
int
_tmain(
int
argc, _TCHAR* argv[])
{
// example
char
ch[100000];
printf
(
"Enter characters: "
);
scanf_s(
"%s"
, ch, 99999);
getchar
();
return
0;
}
- 如果缓冲区大小等于或小于数组的大小,那么输入大于或等于缓冲区大小将不会起任何作用。
- 如果缓冲区大小大于数组大小,则
- 输入小于缓冲区大小的内容可以解决问题,但会出现错误
“运行时检查失败#2–变量‘variable_name’周围的堆栈已损坏。”
- 输入大于缓冲区大小的内容将不起任何作用,并给出相同的错误。
- 输入小于缓冲区大小的内容可以解决问题,但会出现错误
- fscanf_s(): fscanf()和fscanf_s()的区别与scanf()和scanf_s()的区别相同。fscanf_s()是安全函数,安全函数要求将每个c、c、s、s和[type字段的大小作为紧跟在变量后面的参数传递。
Syntax: int fscanf_s( FILE *stream, const char *format ,[argument ]... ); fscanf_s has an extra argument(parameter) where you can specify the buffer size and actually control the limit of the input.
成功后,函数返回已填充的变量数。在输入失败的情况下,在成功读取任何数据之前,会返回EOF。
//C program to illustrate fscanf_s statement
//This program will run on MS Visual studio
#include <stdio.h>
#include <stdlib.h>
int
main()
{
char
s1[10], s2[10], s3[10];
int
year;
// file pointer
FILE
* fp;
// Open file securely
fopen_s(&fp,
"file.txt"
,
"w+"
);
fputs
(
"Hello World its 2017"
, fp);
rewind
(fp);
// Using fscanf_s
fscanf_s(fp,
"%s"
, s1,
sizeof
(s1));
fscanf_s(fp,
"%s"
, s2,
sizeof
(s2));
fscanf_s(fp,
"%s"
, s3,
sizeof
(s3));
fscanf_s(fp,
"%d"
, &year,
sizeof
(year));
printf
(
"String1 |%s|"
, s1);
printf
(
"String2 |%s|"
, s2);
printf
(
"String3 |%s|"
, s3);
printf
(
"Integer |%d|"
, year);
fclose
(fp);
return
(0);
}
输出:
String1 |Hello| String2 |World| String3 |its| Integer |2017|
- sscanf_s(): sscanf_s()是sscanf()的安全函数,安全函数要求将每个c、c、s、s和[type字段的大小作为紧跟在变量后面的参数传递。
Syntax: int sscanf_s(const char *restrict buffer, const char *restrict format, ...); sscanf_s has an extra argument(parameter) where you can specify the buffer size and actually control the limit of the input.
成功后,函数返回已填充的变量数。在输入失败的情况下,在成功读取任何数据之前,会返回EOF。
//C program to illustrate sscanf_s statement
//This program will run on MS Visual studio
#include <stdio.h>
int
main()
{
char
s[] =
"3 red balls 2 blue balls"
;
char
str[10], str2[10];
int
i;
// %*s is used to skip a word
sscanf_s(s,
"%d"
, &i,
sizeof
(i));
sscanf_s(s,
"%*d %*s %*s %*s %s"
, str,
sizeof
(str));
sscanf_s(s,
"%*d %*s %*s %*s %*s %s"
, str2,
sizeof
(str2));
printf
(
"%d %s %s "
, i, str, str2);
return
0;
}
输出:
3 blue balls
注: sscanf_s()只能在Microsoft Visual Studio中工作。
本文由 卡提克·阿胡亚 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。