Before you start, let's take a look at the following C ++ code.
// Definition of function read_file <br/> // return the file's data in string <br/> string read_file (string file_name = "test_data.txt ") <br/>{< br/> ifstream file (file_name.c_str (); <br/> ostringstream file_data; <br/> char temp; <br/> If (file. is_open () = true) <br/>{< br/> while (file. peek ()! = EOF) // traversing the file <br/>{< br/> file. get (temp); // read the data one character by another <br/> file_data <temp; <br/>}< br/> file. close (); // be aware! Remember to close the file! <Br/> return file_data.str (); <br/>}
This function is used to obtain a file name from somewhere else, open the file, read the content one by one into a string, and return the string.
So why should we mention this program? It involves four confusing concepts: file stream, string stream, string, and character array.
1. file stream
1.1 concepts
In C ++, operations on files are performed through the fstream (File
Stream). Therefore, to operate files in this way, you must add the header file fstream. h. My habits # include <fstream>
1.2 Initialization
In actual application, select different classes as needed: If you want to open the class as input, use ifstream to define it. If you want to open the class as output, use ofstream.
Define; if you want to open it in input/output mode, use fstream to define it.
In the above function, I used ifstream to construct a file and store a file name (string array type) as a parameter to read the file.
1.3 open a file
In general, you can open the file explicitly, please Baidu + Google.
1.4 read/write files
Read/write files are divided into text files and binary files. Here we talk about text files.
The reading and writing of text files is very simple: Use the plug-in (<) to output data to the file; Use the extract (>) to input data from the file. For example, test_data.txt contains:
Line1
Line2
We can write as follows:
Ifstream file (file_name.c_str ());
String S1, S2;
File> S1> S2;
S1 = "line1", S2 = "line2 ".
However, in this way, space or line breaks cannot be read, so I use another method: file. Get (temp ). In this way, temp will read them one by one.
1.5 EOF ends File Reading.
2. String stream
2.1 concepts
C ++ introduces three classes: ostringstream, istringstream, and stringstream.
The header file of sstream. h.
The istringstream class is used to perform input operations for C ++-style streaming.
The ostringstream class is used to perform C-style streaming output operations.
The strstream class also supports the input and output operations of C-style streaming.
2.2 understanding
String stream and string comparison: in the standard input and output (CIN, cout), the string always ends with a space or line break, but the string stream can accept multiple characters or strings, including spaces or replace
Line. This is why the above
File_data <temp;
File_data is defined by ostringstream, which is equivalent to a screen buffer and accesses temp characters.
2.3 convert a string into a string, as shown above:
File_data.str ();
3. Character arrays and strings
3.1 tangled Error
Let's take a look at the following example:
Fstream file1 ("1.txt"); // OK!
String S = "1.txt ";
Fstream file2 (s); // error!
Is it really tangled? It is also "1.txt", which can be directly put into the constructor. It cannot be changed to string. What does this mean? String type in C ++ and "..."
The strings are not strictly equal!
Thank you for your article! Address: http://yexin218.javaeye.com/blog/486080
In C, there is no string data type, but a character array is used to save the string. The C string is actually an array of characters ending with null ('/0'), expressed by null.
The end of the string. Note that only the character array ending with null characters is the C string, otherwise it is only a general C character array.
The C string can be initialized with the "=" sign, but cannot be assigned a value to the C string later. To operate the C string, you must use the string defined in the "string" file.
Functions. For example:
// String Initialization
Char A [11] = "huanying ";
// String value assignment
Strcpy (a, "nihao ")
// Obtain the length of a string, excluding '/0'
Strlen ();
Printf ("% s", );
In C, you can also use a character pointer to access a string and use a character pointer to point to the address of the first element that stores the string array.
Char * A = "nihao ";
Printf ("% s", );
In C ++, the string is encapsulated into a data type string. You can directly declare variables and assign values to strings. The following are the differences between string in C string and C ++:
C string String object
Header file name <string> or <string. h>
<String> or <string. h>
Why do we need header files to use string functions to use string classes?
How to declare char name [20]; string name;
How to initialize char name [20] = "nihao"; string name = "nihao ";
Must the string length be declared? Yes No
Is it a null character? Yes No
How to assign strcpy (name, "John"); name = "John ";
Other advantages: faster and easier to use, preferred solution
You can assign a character that is more unavailable than the existing one.
Long string?
C ++ common string functions
Char S1 [] = "I am a student ";
Char S2 [20] = "teacher ";
Char S3 [] = "student ";
Int result;
Char S4 [20], * P;
(1) String Length int strlen (char * Str)
Cout <strlen (S1) <Endl; Output 14
Cout <strlen (S2) <Endl; output 7
(2) string copy char * strcpy (char * str1, char * str2)
Strcpy (S4, S2); // S4 is "teacher"
(3) string connection char * strcat (char * str1, char * str2)
Strcat (S2, S3); // S2 is "teacherstudent"
//
Note: If a character array is used to store strings, The strcat function does not check whether the first array can accommodate the second string, so that the extra string will overflow to the adjacent storage unit.
Question.
(4) string comparison int strcmp (char * str1, char * Str)
// Compare the ASCII value of the corresponding character. If str1> str2, 1 is returned.
Result = strcmp (S2, S3); // result> 0
Result = strcmp (S2, S2); // result = 0
Result = strcmp (S3, S2); // result <0
(5) locate char * strchr (char * STR, char ch)
P = strchr (S1,'s); // locate the position of the returned character in the string; otherwise,-1 is returned.
Strcpy (p, S2); // S1 is "I am a teacher"
(6) check whether there are substrings equal to the other in one string
(7) intercept substrings to form a new string
String Input
(1) Method 1: Use the input operator to fill in a C string variable
For example:
Char A [80];
Cin>;
Note: When reading the C string in this way, the initial blank characters (spaces, tabs, and line breaks) are ignored, and the input will be stopped at the next space or line break.
(2) Method 2: Use the predefined function Getline to obtain the entire line of input (including spaces)
The Getline function has two parameters: the first parameter is used to receive the input C string variable, and the second parameter is used to specify the maximum number of characters that Getline can receive.
For example:
Char A [80];
Cin. Getline (A, 80 );
When the row ends, the input will stop.
C ++ string type input
(1) Method 1: The same as the C string input method.
(2) Method 2: Use the Getline function.
For example:
String;
Getline (CIN, );
Conversion between String object and C string
C strings can be stored in string-type variables, for example:
Char A [] = "nihao ";
String B;
B =;
However, the string object cannot be automatically converted to a C string. Explicit type conversion is required. The string class member function c_str () is required ().
For example:
Strcpy (A, B. c_str ());
String to number Conversion
The atoi function gets a C string parameter and returns the corresponding int value. If the parameter does not match an int value, atoi returns 0. Atoi function in the library where the file is cstdlib
. If the number is too large to be converted to an int value, you can use atol to convert the string to a long value.
For example:
Atoi ("1234"); // returns an integer of 1234.
Atoi ("#123"); // return 0
5. Summary
The difference between character arrays and strings is the real difficulty. All usage or bug solutions are found online, but it is difficult to understand, even the old man can only give different solutions he has met.
Hope someone can see it. Please follow the instructions!