External Files Chapter 8 Interactive versus Batch Processing
External Files Chapter 8 • Interactive versus Batch Processing • Directory Names for External Files • Attaching Streams to External Files Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1
External Files • Reading input from the cin stream is called interactive mode • Data can instead be saved in a file and read from it • This mode of operation is called batch processing Q. What advantages are there to using files? • • Have a record of the values read Can check for errors in the data Can be read many times Can instruct your program to write to a file Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Directory Names for Files To access a file in a program • Must know the file’s directory name • The directory name consists of the path to the file followed by the file name Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Use a loop to process a file • Need to know file format – How information is stored inside a file – Example: “grade. txt” has a student grade (integer) on each line • Does not need to know number of records in file – Can handle this with a while-loop Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Read data from a file: five steps • Include header file <fstream> #include <fstream> • Declare a variable of type ifstream ins; • Open file and test if the file opens correctly ins. open(filename); • Use a loop to read and process each record while (ins >> ) • Close file ins. close(); Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Ifstream fp • ins behaves like cin //read a value from the file into num ins >> num; • Use open statement to connect a stream object to a file • cin reads from standard input stream • ins reads from file input stream Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
The #define directive • We can associate the name of a stream with an external file name #define infile "In. Data. txt" #define outfile "Out. Data. txt" • This association enables us to easily reuse a program with different input and output files. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
The file may have a different format • For example: In grades 2. txt Alice 85 Bob 78 Cathy 89 Douglas 92 Eve 100 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Sample Code for grades 2. txt in. File. open("grades 2. txt"); … while ( in. File >> name >> grade ) { cout << name << ' ' << grade << endl; } OR while ( !in. File. eof() ) { in. File >> name >> grade; cout << name << " => " << grade << endl; } Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9
In functions, ifstream variable has to be passed by reference #include <iostream> int get. Sum(ifstream& fp) #include <fstream> { using namespace std; int num, sum = 0; int get. Sum(ifstream& fp); while (fp >> num) int main() { { ifstream fp; sum += num; fp. open("grades. txt"); } if (!fp) return sum; { cout << "Error opening filen"; } return 0; } int sum = get. Sum(fp); cout << "The sum is " << sum << endl; fp. close(); return 0; Copyright} © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 -10
String. Stream stringstream is a stream class that operates on strings Suppose: string a. String, line; // turn line (type of string) into iss. Line (type of istringstream) istringstream iss. Line(line); while (iss. Line >> a. String) { // do something } Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11
String. Stream #define in. File "In. Data. txt" #define out. File "Out. Data. txt" ofstream outs; //output stream string a. String; … outs. open(out. File); … outs << '*' << a. String << '*'; Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12
C-style strings • C++ programming have two types of strings: • Standard strings • C strings • The C strings are just an array of chars (char*) • Some C++ functions expect C string as parameter. • In order to convert a standard string to a C string, we use the c_str() function Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13
C-style Strings For example: • File open function pre C++11 version requires C string as parameter ins. open(in. File. c_str()); // as in lab 10 outs. open(out. File. c_str()); // as in lab 10 Reference: http: //www. cplus. com/reference/fstream /open/ Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14
- Slides: 14