fscanf_s() Function in C
Difference between fscanf() and fscanf_s() is same as that of scanf() and scanf_s(). fscanf_s() is secure function that require the size of each c, C, s, S and [ ] type field to be passed as an argument immediately following the variable.
Syntax
int fscanf_s( FILE *stream, const char *format ,[argument ]... );
Parameters
- str: string used to retrieve data
- format: string that contains the type specifier(s)
- … (ellipsis): It indicates that the function accepts a variable number of arguments.
Note: fscanf_s has an extra parameter to specify the buffer size and actually control the limit of the input.
Return Value
- On success, the function returns the number of values read successfully.
- In the case of an input failure, before any data could be successfully read, EOF is returned.
Note: fscanf_s will work only in MS Visual studio.
Example: C Program to Illustrate fscanf_s Function
C
// 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|\n" , s1); printf ( "String2 |%s|\n" , s2); printf ( "String3 |%s|\n" , s3); printf ( "Integer |%d|\n" , year); fclose (fp); return (0); } |
Output
String1 |Hello| String2 |World| String3 |its| Integer |2017|
Inbuilt library functions for user Input | sscanf, scanf_s, fscanf_s, sscanf_s
The C Programming Language provides various Inbuilt Library Functions for User Input. In this article, we will learn about sscanf, scanf_s, fscanf_s, sscanf_s Library Functions in C.
Contact Us