How to read a string into a whole line?

Source: Internet
Author: User

In the process of writing code, it is often a line of reading into the string for processing, here I summed up a number of ways for everyone to refer to.

Method One: scanf () read into char[]

How to use:

Char str[1024x768];scanf ("%[^\n]",&str); GetChar ();

Note: In the scanf function, you can use%c to read a character, use%s to read a string, but not ignore whitespace when reading a string, ignore the starting space when you read it, and read a space so that you can read only one word, not the entire line string.

In fact, the scanf function can also accomplish this function, but also more powerful. Here is the main introduction of a parameter,%[], the meaning of this parameter is to read into a character set. [] is a collection of flags, so%[] refers specifically to those characters that are qualified to read into this collection, such as%[a-z] is the input capital letter, and once the characters that are not in this collection are stopped. If the first character of the collection is "^", this means that the characters that are not in the collection after "^" are read, and the characters after the collection after "^" are stopped. Note that the string that is being read in can contain spaces, and the opening spaces are also read in .

Note: If you want to loop through a line more than once from the screen, read the line break in the input buffer after reading a line and reading a character in%c. Otherwise, the next time a row is read, the first encounter ' \ n ', the match is not successful directly returned. Here you can read the newline character with the scanf () or the GetChar () function.

Method Two: GetChar () read into char[]

How to use:

Char str[1024x768]; int i=0;  while ((Str[i]=getchar ())! ='\ n')    i+ +;
GetChar ();

Note: Such a read can also, will be the opening of the space to read in. Finally, you need to consider line breaks and read them using GetChar ().

Method Three: Gets () read into char[]

How to use:

Char str[1024x768];gets (str);

Description: Feel this is a set of multiple GetChar functions, very useful. The function is to read a complete line from the standard input keyboard (read from the standard input, read all the time until a newline character is encountered), put the contents of the read into an array of characters specified in parentheses, and replace the line break ' \ n ' with the null character '% '. you do not need to consider line breaks when reading.

Method four: Getline () read into string or char[]

How to use:

string Str;getline (CIN,STR); // read in string Char str2[1024x768];cin. Getline (STR2,1024x768); // read into a char array

Note: This is a more common method, cin.getline the third parameter represents the spacer, the default is the newline character ' \ n '. read-in does not need to consider the last line break.

Method five: Get () read into char[]

How to use:

Char str3[1024x768];cin. Get (STR3,1024x768); // read into a char array

Note: The Get function needs to consider the last line break when reading , that is, if you read multiple rows of data with GET, you should read ' \ n ' separately, generally using cin.get (str,1024). get (); To read into multiple sets of data.

Alvinzh

Source: http://www.cnblogs.com/AlvinZH/

I Github:https://github.com/pacsiy/jobdu

The copyright belongs to the author Alvinzh and blog Park all, welcome reprint and Commercial, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

How to read a string into a whole line?

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.