strings & text file input
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Strings & Text File Input](https://reader036.vdocuments.us/reader036/viewer/2022062408/56814435550346895db0ce2c/html5/thumbnails/1.jpg)
Strings & Text File Input
CIS 230
15-Feb-06
![Page 2: Strings & Text File Input](https://reader036.vdocuments.us/reader036/viewer/2022062408/56814435550346895db0ce2c/html5/thumbnails/2.jpg)
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.
![Page 3: Strings & Text File Input](https://reader036.vdocuments.us/reader036/viewer/2022062408/56814435550346895db0ce2c/html5/thumbnails/3.jpg)
Strings
• #include <string>
• Character sequence enclosed in double quotes
![Page 4: Strings & Text File Input](https://reader036.vdocuments.us/reader036/viewer/2022062408/56814435550346895db0ce2c/html5/thumbnails/4.jpg)
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”;
![Page 5: Strings & Text File Input](https://reader036.vdocuments.us/reader036/viewer/2022062408/56814435550346895db0ce2c/html5/thumbnails/5.jpg)
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 = “”;
![Page 6: Strings & Text File Input](https://reader036.vdocuments.us/reader036/viewer/2022062408/56814435550346895db0ce2c/html5/thumbnails/6.jpg)
String Input
string message;
cin >> message;
cout << message;
This may have problems….
![Page 7: Strings & Text File Input](https://reader036.vdocuments.us/reader036/viewer/2022062408/56814435550346895db0ce2c/html5/thumbnails/7.jpg)
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.
![Page 8: Strings & Text File Input](https://reader036.vdocuments.us/reader036/viewer/2022062408/56814435550346895db0ce2c/html5/thumbnails/8.jpg)
String Input Using >>
string firstName;
string lastName;
cin >> firstName >> lastName;
• Input stream:
Joe Hernandez 23
Results:“Joe” “Hernandez”firstName lastName
![Page 9: Strings & Text File Input](https://reader036.vdocuments.us/reader036/viewer/2022062408/56814435550346895db0ce2c/html5/thumbnails/9.jpg)
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);
![Page 10: Strings & Text File Input](https://reader036.vdocuments.us/reader036/viewer/2022062408/56814435550346895db0ce2c/html5/thumbnails/10.jpg)
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
![Page 11: Strings & Text File Input](https://reader036.vdocuments.us/reader036/viewer/2022062408/56814435550346895db0ce2c/html5/thumbnails/11.jpg)
String Input: getline
string firstName;
string lastName;
getline (cin, firstName);
getline (cin, lastName);
• Input stream:
Joe Hernandez 23
Results:
“Joe Hernandez 23” ?firstName lastName
![Page 12: Strings & Text File Input](https://reader036.vdocuments.us/reader036/viewer/2022062408/56814435550346895db0ce2c/html5/thumbnails/12.jpg)
String Operations
![Page 13: Strings & Text File Input](https://reader036.vdocuments.us/reader036/viewer/2022062408/56814435550346895db0ce2c/html5/thumbnails/13.jpg)
Text File Input
• Text file is an ASCII file
• Handles:– Integers– Floating point numbers– Characters
![Page 14: Strings & Text File Input](https://reader036.vdocuments.us/reader036/viewer/2022062408/56814435550346895db0ce2c/html5/thumbnails/14.jpg)
Text File Input
• #include <fstream>
• Declare a local filename, of type fstream:Examples:
fstream fileName;
fstream inFile;
![Page 15: Strings & Text File Input](https://reader036.vdocuments.us/reader036/viewer/2022062408/56814435550346895db0ce2c/html5/thumbnails/15.jpg)
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
![Page 16: Strings & Text File Input](https://reader036.vdocuments.us/reader036/viewer/2022062408/56814435550346895db0ce2c/html5/thumbnails/16.jpg)
Example: Integer 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.dat345456213578
98222865909145
500<eof>
Output:The values in the file were345 456 213 578 98 222 865 909 145 500end of file
![Page 17: Strings & Text File Input](https://reader036.vdocuments.us/reader036/viewer/2022062408/56814435550346895db0ce2c/html5/thumbnails/17.jpg)
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();}
![Page 18: Strings & Text File Input](https://reader036.vdocuments.us/reader036/viewer/2022062408/56814435550346895db0ce2c/html5/thumbnails/18.jpg)
Mixed Data
• Input must match the file layout
Example file:
3124 Hammer 12.95
2784 HandSaw 19.99
1447 Wrenches 7.63
integer string float2 spaces 1 space
![Page 19: Strings & Text File Input](https://reader036.vdocuments.us/reader036/viewer/2022062408/56814435550346895db0ce2c/html5/thumbnails/19.jpg)
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;}
![Page 20: Strings & Text File Input](https://reader036.vdocuments.us/reader036/viewer/2022062408/56814435550346895db0ce2c/html5/thumbnails/20.jpg)
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);}
![Page 21: Strings & Text File Input](https://reader036.vdocuments.us/reader036/viewer/2022062408/56814435550346895db0ce2c/html5/thumbnails/21.jpg)
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