C String formatting
sscanf() Used to format strings
And scanf() similar , but scanf() Keyboard as input , but sscanf() Fixed string as input source
int sscanf(char *buffer, const char *format, [argument]...); //
format: One or more {%[*] [width] [{h | I | I64 | L}]type | ' ' | '\t' | '\n' | wrong % Symbol }
// * Indicates that the data is skipped and not read in , as (%*d,%*s) Do not read integers , Do not read string // {a|b|c}
express a,b,c Choose one ,[d] Do you have any d Fine // width Represents the width of the read // type express %s,%d And so on // %*[width] [{h
| l | I64 | L}]type Indicates that those meeting the condition are filtered out , The value is not written to the target parameter
sprintf() And snprintf()
Writes formatted data to a string ,snprintf() More secure
int sprintf( char *buffer, const char *format, [argument]...); int
snprintf(char *buffer, size_t size, const char *format, [argument] ...); //
buffer:char Type pointer , The buffer that points to the string to be written . // size: Represents the current buffer Maximum capacity of // format: format string .
// [argument]...: Optional parameters , It can be any type of data . // Return value : String length (strlen)
C++ String formatting
stringstream flow
stringstream ss; string a; ss.setf(); // Set some properties ss << setprecision(15) <<
a; // Set the number of bits to read , And will a Save to data stream string str; ss >> str; // Save data output from the stream to str in int
num; ss << num; // Convert the data in the stream to integer and save it in num in // note: stringstream
When parsing objects , It is separated by a space and a return key , If you want to use other characters as separators , Need to use getline() function
stringstream Read multiple rows of data
ifstream fin(path, std::ios::in); // File stream while (!fin.eof()) { string
tmp; getline(fin, tmp); // Read a line and put it in tmp in } stringstream ss(str); //
Create content as str Character stream for string c; while (getline(ss, c, ',')) { // Read from the input stream to , Split the string and save it c in
cout << c << endl; }
Technology