250 likes | 364 Views
Chapter 11 Standard C++ Strings and File I/O. Dept of Computer Engineering Khon Kaen University. Outline. Reading inputs Formatted input Unformatted input The Standard C++ String Type Files String Streams. The Input Operator.
E N D
Chapter 11Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University
Outline • Reading inputs • Formatted input • Unformatted input • The Standard C++ String Type • Files • String Streams 178110: Computer Programming (II/2546)
The Input Operator • Input passes through an istream object and output passes through an ostream object • The istream class defines the behavior of objects like cin • The most common behavior is the use of the extraction operator >> (also called the input operator) 178110: Computer Programming (II/2546)
Formatted Input • It has two operands • The istream object from which it is extracting characters • The object to which it copies the corresponding value formed from those characters • This process of forming a typed value from raw input characters is called formatting 178110: Computer Programming (II/2546)
Example: Formatted Input int main() { int n; cin >> n; cout << "n ='" << n << "'" << endl; } • What is the output when you enter “ 46”? • n=’46’ 178110: Computer Programming (II/2546)
The Extraction Operator >> • The extraction operator >> formats the data that it receives through its input stream • It extracts characters from the stream and uses them to form a value of the same type as its second operand • It ignores all whitespace characters that precede the characters it uses • Can you use the extraction operator to read whitespace characters? • No, we must use an unformatted input operation 178110: Computer Programming (II/2546)
The Expression cin >> x • The operator expression cin >> x has a value that can be interpreted in a condition as boolean (true or false) • When the expression will be true? • When the reading of the input is successful • This allows such an expression to be used to control a loop 178110: Computer Programming (II/2546)
Using the Extraction Operator int n; while (cin >> n) cout << "n= " << n << endl; • What are the outputs of these inputs? • 46 • 22 44 66 88 • 33, 55, 77, 99 178110: Computer Programming (II/2546)
Unformatted Input • The <iostream> file defines several functions inputting characters and C-strings that do not skip over whitespace • The most common are the cin.get() function for reading individual characters and the cin.getline() function for reading C-strings 178110: Computer Programming (II/2546)
Inputting Characters with cin.get() Function while (cin.get(c)) { if (c >= 'a' && c <= 'z') c += 'A' - 'a'; // capitalize c cout.put(c); if (c == '\n') break; } • What are the outputs of these inputs? • 123,456 • Hello kku 178110: Computer Programming (II/2546)
Inputting C-Strings with cin.getline() Function const int LEN=32; // maximum word length const int SIZE=10; // array size typedef char Name[LEN]; // defines Name to be a C-string type int main() { Name king[SIZE]; int n = 0; while (cin.getline(king[n++], LEN) && n<= SIZE) ; --n; // now n = the number of names read for (int i=0; i < n; i++) cout << "\t" << i+1 << ". " << king[i] << endl; } 178110: Computer Programming (II/2546)
The Standard C++ string Type • Standard C++ defines its string type in the <string> header file • Objects of type string can be declared and initialized in several ways • string s1; // s1 contains 0 characters • string s2 = “New York”; // s2 has 8 characters • string s3(60, ‘*’); // s3 has 60 ‘*’ characters • string s4 = s3; // s4 has 60 ‘*’ characters • string s5(s2, 4, 2); // s5 = ‘Yo’ 178110: Computer Programming (II/2546)
C++ string Type • C++ strings have a getline() function that works almost the same way as the cin.getline() function for C-strings • string s6 = “ABCDEFG”; • string s7; • getline(cin,s7); // read an entire line of characters into s7 • They also use the subscript operator the same way that C-strings do: • char c = s6[2]; // assign ‘C’ to character c • s6[4] = ‘*’; // s6 = ABCD*FG 178110: Computer Programming (II/2546)
C++ string Type • C++ strings can be converted to C-strings like this: • const char* cs = s6.c_str(); • The C++ string class also defines a length() function that can be used like this to determine how many characters are stored in a string • cout << s6.length() << endl; 178110: Computer Programming (II/2546)
C++ string Type Operators • C++ strings can be compared using the relational operators like fundamental types: • string s8 = “hello”; • string s9 = “waddee”; • if (s8 < s9) cout << “hello precedes waddee”; else cout << “hello does not precede waddee”; 178110: Computer Programming (II/2546)
C++ string Type Operator • You can also concatenate and append strings using the + and += operators; • s6 = s6 + “HIJK”; // s6 = “ABCD*FGHIJK” • s2 += s5; // s2 = “New YorkYo” • The substring() function is used like this • s4 = s6.substr(5,3); // s4 = “FGH” • The erase() function is used like this • s6.erase(4, 2); // s6 = “ABCDGHIJK” 178110: Computer Programming (II/2546)
C++ string Type Operators • The replace() function is used like this • s6.replace(5,2, “xyz”); // s6 = “ABCDGxyzJK” • The find() function returns the index of the first occurrence of a given substring • string s10 = “Khon Kaen U” • cout << s10.find(“on”) << endl; // print 2 • cout << s10.find(“en”) << endl; // print 7 178110: Computer Programming (II/2546)
Using the C++ string Type int main() { string word; while (cin >> word) { cout << "before: " << word << endl; int numChars = word.length(); for (int i = 0; i < numChars; i++) { if (is_vowel(word[i])) word.replace(i,1,"*"); } cout << "after: " << word << endl; } } bool is_vowel(char c) { return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'); } 178110: Computer Programming (II/2546)
Files • File processing in C++ is very similar to ordinary interactive input and output because the same kind of stream objects are used • Input from a file is managed by an ifstream object the same way that input from the keyboard is managed by the istream object cin • Output to a file is managed by an ofstream object the same way that output to the monitor or printer is managed by the ostream object cout 178110: Computer Programming (II/2546)
Files • Unlike istream and ostream, ifstream and ofstream must be declared explicitly and initialized with the external name of the file which they manage • You also have to #include the <fstream> header file that defines ifstream and ofstream 178110: Computer Programming (II/2546)
Capitalizing All the Words in a Text File #include <fstream> … int main() { ifstream infile("input.txt"); ofstream outfile("output.txt"); string word; char c; while (infile >> word) { if (word[0] >= 'a' && word[0] <= 'z') { word[0] = word[0] + 'A' - 'a';} outfile << word << " "; } } 178110: Computer Programming (II/2546)
Merging Two Sorted Data Files bool more(ifstream& fin, int& n) { if (fin >> n) return true; else return false; } bool copy(ofstream& fout, ifstream& fin, int& n) { fout << " " << n; return more(fin, n); } 178110: Computer Programming (II/2546)
Merging Two Sorted Data Files int main() { ifstream fin1("north.dat"); ifstream fin2("south.dat"); ofstream fout("combined.dat"); int n1, n2; bool more1 = more(fin1, n1); bool more2 = more(fin2, n2); while (more1 && more2) {if (n1 < n2) more1 = copy(fout, fin1, n1); else more2 = copy(fout, fin2, n2);} while (more1) more1 = copy(fout, fin1, n1); while (more2) more2 = copy(fout, fin2, n2); fout << endl; } 178110: Computer Programming (II/2546)
String Streams • A string stream is a stream object that allows a string to be used as an internal text file • This is also called in-memory I/O • String streams are quite useful for buffering input and output • Their types isstringstream and ostringstream are defined in the <sstream> header file 178110: Computer Programming (II/2546)
Using an Output String Stream int main() { string s; istringstream iss("ABCDEFG 44"); string s; iss >> s; int n; iss >> n; cout << “s is “ << s << “ n is “ << n << endl;} 178110: Computer Programming (II/2546)