210 likes | 303 Views
Strings & Text File Input. CIS 230 15-Feb-06. Quiz. Write a function Prototype for the function swap_values that takes two integers by reference and does not return. Write a function call for swap_values . Use the values num1 , num2 as arguments.
E N D
Strings & Text File Input CIS 230 15-Feb-06
Quiz • Write a function Prototype for the function swap_valuesthat takes two integers by reference and does not return. • Write a function call for swap_values. Use the values num1, num2 as arguments. • Write a function header for the function swap_valuesthat takes three integers, num1, num2 and does not return.
Strings • #include <string> • Character sequence enclosed in double quotes
Declaring & Initializing Strings • string objectName = value; • string str1 = “Good Morning”; • string str2 = str1; • string str3 = str1 + str2; • string objectName(stringValue); • string str4(“Hot”); • string str5(str4 + “ Dog”); • string objectName(str, n); • string str6(“Good Morning”); • string str7(str6, 5); //str7 = “Morning”;
Declaring & Initializing Strings • string objectName(str, n, p); • string str8(“Good Morning”); • string str9(str8, 5, 2); //str9 = “Mo”; • string objectName(n, char); • string str10(5, ‘*’); //str10 = “*****”; • string objectName; • string message; //message = “”;
String Input string message; cin >> message; cout << message; This may have problems….
Extraction Operator >> • >> skips any leading whitespace characters • >> stops at (before) the first trailing whitespace character • trailing whitespace character is left in stream, and will become “next” leading whitespace character.
string firstName; string lastName; cin >> firstName >> lastName; Input stream: Joe Hernandez 23 Results: String Input Using >> “Joe” “Hernandez” firstName lastName
getline() Function • >> cannot be used to input a string with blanks in it. • Use getline(inFileStream, str) • First argument is an input stream variable (cin), second is the string variable. string message; getline(cin, message);
getline(inFileStream, str) • getline does not skip leading whitespace characters • getline reads all characters (including blanks) into the string. • getline stops when it reaches ‘\n’ • ‘\n’ is not stored in the string variable
string firstName; string lastName; getline (cin, firstName); getline (cin, lastName); Input stream: Joe Hernandez 23 Results: String Input: getline “Joe Hernandez 23” ? firstName lastName
Text File Input • Text file is an ASCII file • Handles: • Integers • Floating point numbers • Characters
Text File Input • #include <fstream> • Declare a local filename, of type fstream: Examples: fstream fileName; fstream inFile;
Commands • fileName.open(“actualName.dat”, ios::in); • fileName.eof() • fileName >> var; • var = filename.get(); • filename.close(); Makes the connection Tells it’s for input // Returns true when it reaches the end of file // Just like cin, except from the file // (>> skips white spaces for char input) // for char input, includes white spaces // Closes an opened file
#include <iostream> #include <fstream> using namespace std; int main() { int a; fstream inFile; inFile.open(“int.dat”, ios::in); cout << “The values in the file were\n”; while (!inFile.eof()) { inFile >> a; cout << a << “ “; } cout << “\nend of file\n”; inFile.close(); } int.dat 345 456 213 578 98 222 865 909 145 500<eof> Example: Integer file • Output: • The values in the file were • 456 213 578 98 222 865 909 145 500 • end of file
Example: String as Filename #include <iostream> #include <fstream> #include <string> using namespace std; int main() { int a; string fileName = “int.dat”; fstream inFile; inFile.open(inFile.c_str(), ios::in); … inFile.close(); }
Mixed Data • Input must match the file layout Example file: 3124 Hammer 12.95 2784 HandSaw 19.99 1447 Wrenches 7.63 integer string float 1 space 2 spaces
int main() { int partNum; string part; float price; string file = "parts.dat"; fstream inFile; inFile.open(file.c_str(), ios::in); cout << "\nPart Number" << setw(12) << "Price" << setw(18) << "Description\n\n"; inFile >> partNum >> part >> price; while (!inFile.eof()) { cout << setw(8) << partNum << setw(8) << '$' << setw(8) << price << setw(5) << " " << part << endl; inFile >> partNum >> part >> price; } inFile.close(); return 0; }
File existence • fail() string file; cout << "Please enter a file name: "; getline(cin, file); fstream inFile; inFile.open(file.c_str(), ios::in); if (inFile.fail()) { cout << "Sorry, I can't find the file\n"; exit(1); }
Located in: /class-files/samples/strings/ stringpractice.cpp stringinput.cpp stringoperations.cpp Located in: /class-files/samples/files ask.cpp infile.cpp mixed.cpp int.dat parts.dat Example code: