Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings & Text File Input CIS 230 15-Feb-06. Quiz 1.Write a function Prototype for the function swap_values that takes two integers by reference and does.

Similar presentations


Presentation on theme: "Strings & Text File Input CIS 230 15-Feb-06. Quiz 1.Write a function Prototype for the function swap_values that takes two integers by reference and does."— Presentation transcript:

1 Strings & Text File Input CIS 230 15-Feb-06

2 Quiz 1.Write a function Prototype for the function swap_values that takes two integers by reference and does not return. 2.Write a function call for swap_values. Use the values num1, num2 as arguments. 3.Write a function header for the function swap_values that takes three integers, num1, num2 and does not return.

3 Strings #include Character sequence enclosed in double quotes

4 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”;

5 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 = “”;

6 String Input string message; cin >> message; cout << message; This may have problems….

7 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.

8 String Input Using >> string firstName; string lastName; cin >> firstName >> lastName; Input stream: Joe Hernandez 23 Results: “Joe”“Hernandez” firstNamelastName

9 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);

10 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

11 String Input: getline string firstName; string lastName; getline (cin, firstName); getline (cin, lastName); Input stream: Joe Hernandez 23 Results: “Joe Hernandez 23”? firstNamelastName

12 String Operations

13 Text File Input Text file is an ASCII file Handles: –Integers –Floating point numbers –Characters

14 Text File Input #include Declare a local filename, of type fstream: Examples: fstream fileName; fstream inFile;

15 Commands fileName.open(“actualName.dat”, ios::in); fileName.eof() fileName >> var; var = filename.get(); filename.close(); Makes the connectionTells 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

16 Example: Integer file #include 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 Output: The values in the file were 345 456 213 578 98 222 865 909 145 500 end of file

17 Example: String as Filename #include using namespace std; int main() { int a; string fileName = “int.dat”; fstream inFile; inFile.open(inFile.c_str(), ios::in); … inFile.close(); }

18 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 2 spaces 1 space

19 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; }

20 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); }

21 Example code: 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


Download ppt "Strings & Text File Input CIS 230 15-Feb-06. Quiz 1.Write a function Prototype for the function swap_values that takes two integers by reference and does."

Similar presentations


Ads by Google