Read data separated by commas (,) in C ++.

Source: Internet
Author: User
Tags strtok

What we see on the Internet is reading the entire line and processing it!

Method

Read to an object of the string type, and then replace "," in the string with a space or other separator;

Then, use the modified string to initialize a stringstream and read data to the target object one by one.

2011.6.25

I can see a good person reply to me, saying that the strtok () function can be used, so I searched for related knowledge about this function and learned to learn it ~

Prototype: char * strtok (char * s, char * delim );
Function: Splits a string into a group of strings. S is the string to be decomposed, and delim is the separator string. Essentially, strtok searches for characters contained in delim in S and replaces them with null ('\ 0') until the entire string is searched.
Note: during the first call, s points to the string to be decomposed, and thenTo call another call, set S to null.. Strtok searches for characters contained in delim in S and replaces them with null ('\ 0') until the entire string is searched.
Return Value: Split strings starting with S. If no split string exists, null is returned. All delim characters are filtered out, And the filtered characters are set as a separate node.

Note: The original string will be changed !!!

Example:

# Include <stdio. h>
# Include <string. h>
Int main (INT argc, char ** argv)
{
Char buf1 [] = "AAA, A, BBB-C, ee | ABC ";
/* Establish string and get the first token :*/
Char * token = strtok (buf1, ",-| ");
While (Token! = NULL)
{
/* While there are tokens in "string "*/
Printf ("% s", token );
/* Get next token :*/
Token = strtok (null, ",-| ");
}
Return 0;
}

 

However, this function cannot solve the problem I want to solve: Because I want to split a series of data (such as 30, 2.5, etc.), after which, the string is obtained.

The C/C ++ language provides several standard library functions that can convert strings to any type (integer, long integer, floating point type, etc ). The following describes the methods and descriptions of each function.

Atof (): converts a string to a double-precision floating point value.

Atoi (): converts a string to an integer.

Atol (): converts a string to a long integer.

Strtodd (): converts a string to a double-precision floating-point value and reports all the remaining numbers that cannot be converted.

Strtol (): converts a string to a long integer and reports all the remaining numbers that cannot be converted.

Strtoul (): converts a string to an unsigned long integer and reports all the remaining numbers that cannot be converted.

Note:The disadvantage is that if the conversion fails, 0 is returned (the result is the same as if the value is 0)

Another method is introduced: Use the sscanf function (corresponding to the sprintf function ).

Example:

Char STR [] = "15.455 ";
Int I;
Float FP;
Sscanf (STR, "% d", & I); // converts a string to an integer I = 15
Sscanf (STR, "% F", & FP); // convert the string to a floating point fp = 15.455000
// Print
Printf ("INTEGER: = % d", I + 1 );
Printf ("real: = % F", FP + 1 );
Return 0;

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.