140 likes | 158 Views
External Files. Chapter 8 Interactive versus Batch Processing Directory Names for External Files Attaching Streams to External Files. External Files. R eading input from the cin stream is called interactive mode D ata can instead be saved in a file and read from it
E N D
External Files Chapter 8 • Interactive versus Batch Processing • Directory Names for External Files • Attaching Streams to External Files
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
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
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
Read data from a file: five steps • Include header file <fstream> #include <fstream> • Declare a variable of type ifstream 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();
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
The #define directive • We can associate the name of a stream with an external file name #define infile "InData.txt" #define outfile "OutData.txt" • This association enables us to easily reuse a program with different input and output files.
The file may have a different format • For example: In grades2.txt Alice 85 Bob 78 Cathy 89 Douglas 92 Eve 100
Sample Code for grades2.txt inFile.open("grades2.txt"); … while ( inFile >> name >> grade ) { cout << name << ' ' << grade << endl; } OR while ( !inFile.eof() ) { inFile >> name >> grade; cout << name << " => " << grade << endl; }
int getSum(ifstream& fp) { int num, sum = 0; while (fp >> num) { sum += num; } return sum; } #include <iostream> #include <fstream> using namespace std; int getSum(ifstream& fp); int main() { ifstream fp; fp.open("grades.txt"); if (!fp) { cout << "Error opening file\n"; return 0; } int sum = getSum(fp); cout << "The sum is " << sum << endl; fp.close(); return 0; } In functions, ifstream variable has to be passed by reference
StringStream stringstream is a stream class that operates on strings Suppose: string aString, line; // turn line (type of string) into issLine (type of istringstream) istringstream issLine(line); while (issLine >> aString) { // do something }
StringStream #define inFile "InData.txt" #define outFile "OutData.txt" ofstream outs; //output stream string aString; … outs.open(outFile); … outs << '*' << aString << '*';
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
C-style Strings For example: • File open function pre C++11 version requires C string as parameter ins.open(inFile.c_str()); // as in lab 10 outs.open(outFile.c_str()); // as in lab 10 Reference: http://www.cplusplus.com/reference/fstream/fstream/open/