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

advertisement
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 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.
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 Input Using >>
string firstName;
string lastName;
cin >> firstName >>
lastName;
• Input stream:
Joe Hernandez 23
Results:
“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 Input: getline
string firstName;
string lastName;
getline (cin, firstName);
getline (cin, lastName);
• Input stream:
Joe Hernandez 23
Results:
“Joe Hernandez 23”
firstName
?
lastName
String Operations
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);
Makes the connection
• fileName.eof()
• fileName >> var;
// Returns true when it reaches the end of file
// Just like cin, except from the file
// (>> skips white spaces for char input)
• var = filename.get();
• filename.close();
Tells it’s for input
// for char input, includes white spaces
// Closes an opened file
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”;
Output:
inFile.close();
The values in the file were
}
345 456 213 578 98
end of file
int.dat
345
456
213
578
98
222
865
909
145
500<eof>
222
865
909
145
500
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
2 spaces 1 space
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);
}
Example code:
Located in:
/classfiles/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