In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what are the differences between C language scanf,fscanf and sscanf". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Catalogue
I. scanf, fscanf and sscanf
1.scanf
2.fscanf
3.sscanf
II. Printf, fprintf and sprintf
1.printf
2.fprintf
3.sprintf
I. scanf, fscanf and sscanf1.scanf
First of all, scanf we are very familiar with, he is to enter data from the keyboard, to be exact:
Read formatted data from standard input (keyboard).
Int main () {int x = 0; scanf ("% d", & x); return 0;} 2.fscanf
Fscanf is a little more advanced than scanf. It can be said that fscanf includes the function of scanf. The definition of this function is as follows:
Read formatted data from all input streams.
We can take a look at the explanations and parameters in fscanf-cpulspuls:
Definition: read data from the stream and store them in the location pointed to by the additional parameters according to the parameter format. The additional parameter should point to an object of the assigned type specified by the corresponding format specifier in the format string.
Int fscanf (FILE * stream, const char * format,...)
If you don't understand the parameters here, we can compare them to learn. We can open the description of scanf to take a look at the comparison:
In fact, fscanf has an extra parameter of FILE *, that is, the address parameter of the open file, so when we use it, we can first write it out like scanf, and then add a parameter of FILE * before it.
Example:
We can open the file, read the data in the input stream, and then print it out to see:
/ / fscanfstruct S {int n; double d;}; int main () {struct S s = {0}; FILE* pf = fopen ("data.txt", "r"); / / text file data.txt if (NULL = = pf) {perror ("fopen"); return-1 } / / read the file fscanf (pf, "% d% lf", & (s.n), & (s.d)); printf ("% d% lf\ n", s.n, s.d); / / close the file fclose (pf); pf = NULL;} 3.sscanf
As an old rule, let's check the definition of this function:
The meaning here is to read the formatted data from the string, that is, the data we enter, sscanf will be converted into the form of a string. That is:
Reads a formatted data from a string.
Int sscanf (const char * s, const char * format,...)
For the parameters we can compare again, the previous fscanf is an extra FILE * pointer, and now it is changed to a character pointer, that is, to read a data from the character pointer to the later formatted data.
Example:
Take the data from the character array into the structure and print it out (here you need to understand the following sprintf and then look at it together):
Struct S {int n; double d; char name [10];}; int main () {char arr [100] = {0}; struct S tmp = {0}; struct S s = {100,3.14, "zhangsan"} / / convert a formatted data to a string sprintf (arr, "% d% lf% s", s.n, s.d, s.name); / / print printf ("% s\ n", arr) / / extract a formatted data sscanf (arr, "% d% lf% s", & (tmp.n), & (tmp.d), tmp.name) from the string in arr; / / print printf ("% d% lf% s\ n", tmp.n, tmp.d, tmp.name); return 0;} II. Printf, fprintf and sprintf1.printf
Printf is a print function that outputs from the keyboard. For it, it is:
Send formatted data to standard output (screen)
Int main () {int x = 0; printf ("% d", x); return 0;} 2.fprintf
Fprintf is also a little more advanced than printf, it can be exported to more places, that is, it includes the function of printf, which is defined as:
Output formatted data to all output streams (screens / files)
Similarly, we can first check fprintf-cplusplus:
Definition: writes a C string pointed to in format to the stream. If the format includes format specifiers (subsequences starting with%), the formatted additional parameters are formatted and inserted into the result string, replacing their respective specifiers.
Int fprintf (FILE * stream, const char * format,...)
After the same comparison, we find that fprintf and printf have a difference in the parameter of FILE *, so we can also write according to the format of printf, and then add the parameters to make good use of fprintf.
Example:
Here we output some values to the file, that is, when we finish running the code, we will have this data on the file:
/ / fprintf (written in a certain format) struct S {int n; double d;}; int main () {struct S s = {100,3.14}; FILE* pf = fopen ("data.txt", "w"); / / write text file data.txt if (NULL = = pf) {perror ("fopen"); return-1 } / / write file fprintf (pf, "% d% lf", s.n, s.d); / / close file fclose (pf); pf = NULL;} 3.sprintf
For ssprintf, let's first look at the definition:
Interpretation: when using formatting on printf, the same text is used to form a string, but the content is not printed out, but is stored in the C string in the buffer pointed to by str.
In fact, what this means is:
Converts the formatted data output into a string.
Int sprintf (char * str, const char * format,...)
For this function parameter, we refer to the printf transformation to find that there is an extra character pointer, that is, to read the data from the formatting of the later output to the previous character pointer. Then the output is a string.
Example:
Print multiple types of data for structures:
Struct S {int n; double d; char name [10];}; int main () {char arr [100] = {0}; struct S tmp = {0}; struct S s = {100,3.14, "zhangsan"} / / convert a formatted data to the string sprintf (arr, "% d% lf% s", s.n, s.d, s.name); / / print printf ("% s\ n", arr); return 0;}
We printed it in% s and printed it out:
This is the end of the content of "what is the difference between C language scanf,fscanf and sscanf". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.