1 / 21

Strings & Text File Input

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.

pippa
Télécharger la présentation

Strings & Text File Input

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. Strings • #include <string> • 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 firstName; string lastName; cin >> firstName >> lastName; Input stream: Joe Hernandez 23 Results: String Input Using >> “Joe” “Hernandez” firstName lastName

  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 firstName; string lastName; getline (cin, firstName); getline (cin, lastName); Input stream: Joe Hernandez 23 Results: String Input: getline “Joe Hernandez 23” ? firstName lastName

  12. String Operations

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

  14. Text File Input • #include <fstream> • 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 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

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

  17. 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(); }

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

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

More Related