Use of sscanf and sstream

Source: Internet
Author: User

 SscanfIs a runtime function, the prototype is very simple:
IntSscanf (
ConstChar* Buffer,
     ConstChar* Format[,
   Argument]...
);
Its powerful functions are reflected in the support for format and type conversion.

The format can be one or more {% [*] [Width] [{H | L | I64 | L}] Type       |       ' ' |       '\ T' | '\ N' | Non-% symbol },
Note: {A | B | c} indicates A, B, and C. Select [d], which indicates D or D. 
Width: width, which can be ignored. Its usage is as follows:
Const       Char Sourcestr [] = "Hello, World ";
Char Buf [10] = {0 };
Sscanf (sourcestr, "% 5s ", Buf );         // % 5 s, only 5 Characters
Cout < Buf < Endl;
Result: Hello

{H|L|I64|L}: The parameter size. Generally, h indicates the size of a single byte, And I indicates 2 bytes.Size, L indicates 4-byte size (with the exception of double), and l64 indicates 8-byte size.
Type: This is a lot, that is, % s, % d and so on.
Special:

% * [Width][{H|L|I64|L}] Type   It indicates that the values that meet the condition are filtered out and will not be written to the target parameter. For example:
Const   CharSourcestr []="Hello,World ";
CharBuf [10]={0 };
Sscanf (sourcestr,"% * S % s ",Buf );    // % * S indicates that the first matching % s is filtered out, that is, hello is filtered out.
Cout<Buf <Endl;
Result: World

Collection operations are supported:
  % [A-Z]   Match any character from A to Z, greedy (as many as possible)
  % [AB ']Matching A, B, and ', greedy
  % [^ A]Match any character other than a, greedy
For example, if you specify a string iios/12ddwdff @ 122 and obtain the string between/and @, filter out "iios/" first, then, send a string of content other than '@' to the Buf:

Sscanf ("iios/12ddwdff @ 122", "% * [^/]/% [^ @]", Buf );

Printf ("% s \ n", Buf); Result: 12 ddwdff
Sscanf can extract integers, floating-point numbers, and strings from strings. It is easy to use, especially for integers and floating-point numbers.

 StringstreamAs the name suggests, it is the input and output stream of a string, which is similar to fstream.
Header file: # include <sstream>

 

Stringstream is usually used for data conversion. Compared with the C-database conversion, it is more secure, automatic, and direct.

Example 1: Convert the basic data type from int to string
# Include <string>
# Include <sstream>
# Include <iostream>
Int main ()
{
   STD: stringstream stream;
   STD: String result;
   Int I = 1000;
   Stream <I; // input int streams
   Stream> result; // extract the int value inserted earlier from stream
   STD: cout <result <STD: Endl; // print the string "1000"
}
Running result: print the string "1000"

Example 2: In addition to basic type conversion, char * conversion is also supported.
# Include <sstream>
# Include <iostream>
Int main ()
{
   STD: stringstream stream;
   Char result [8];
   Stream <8888; // insert 8888 to stream
   Stream> result; // extract the value from stream to result
   STD: cout <result <STD: Endl; // The screen displays "8888"
}
Screen Display "8888"

Example 3: When performing multiple conversions, you must call the stringstream member function clear ().
# Include <sstream>
# Include <iostream>
Int main ()
{
   STD: stringstream stream;
   Int first, second;
   Stream <"456"; // insert a string
   Stream> first; // convert to int
   STD: cout <first <STD: Endl;
   Stream. Clear (); // You must clear the stream before performing multiple conversions.
   Stream <true; // insert bool Value
   Stream> second; // extract int
   STD: cout <second <STD: Endl;
}
Result of running clear:
456
1
No result for running clear:
456
8800090900

Note the differences between stringstream and sscanf:

 

 If STR [] = "one two three four "; 

 

 If read as follows: Char word [20] [20]; for (I = 0; I <n; I ++) sscanf (STR, "% s ", word [I]); 

 

 Every word [I] you read is the word "one", and stringstream reads the words in sequence. 

 

 The reason is very simple. stringstream is to operate on a fixed string, and there is a pointer inside to mark which to read, and sscanf reads STR from the beginning each time. 

 

 Although it is not recommended to use the input and output streams in C ++ in ACM, the problem of small data size occasionally can greatly improve the speed and accuracy of code writing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.