- 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
#include int main() { char a[10]; printf("Please enter your name : \n"); //scanf statement scanf("%s", a); printf("You entered: \n%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
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 \n", 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
#include 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|\n", s1 ); printf("String2 |%s|\n", s2 ); printf("String3 |%s|\n", s3 ); printf("Integer |%d|\n", 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表示“红色”,它将很好用,但是如果用户输入了3个以上的字符, scanf将开始写入不属于颜色的内存中。 C不会捕获或警告您,它可能会也可能不会使程序崩溃,这取决于是否有人尝试在不属于颜色的内存插槽中进行访问和写入。这就是scanf_s起作用的地方。 scanf_s检查用户输入是否适合给定的内存空间。// C program to illustrate sscanf_s statement // scanf_s() will only work in Microsoft Visual Studio. #include
#include int main() { char a[5]; // sizeof(a) is buffer size scanf_s("%s", a, sizeof(a)); printf("\n%s ", a); return 0; } 输入:
Red
输出:
Red
输入:
Yellow
输出:
No Output
说明缓冲区大小和数组大小之间的关系。
C
// C program // consumes the Enter key // (newline character) pressed after input #include
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
#include 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|\n", s1); printf("String2 |%s|\n", s2); printf("String3 |%s|\n", s3); printf("Integer |%d|\n", year); fclose(fp); return(0); } 输出:
String1 |Hello| String2 |World| String3 |its| Integer |2017|
- sscanf_s():sscanf_s()是的sscanf()和安全功能的安全函数要求每个C,C的大小,S,S和[类型字段被作为立即可变以下内容的参数传递。
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
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 \n", i, str, str2); return 0; } 输出:
3 blue balls
注意: sscanf_s()仅在Microsoft Visual Studio中有效。