1 / 14

External Files

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

Download Presentation

External Files

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. External Files Chapter 8 • Interactive versus Batch Processing • Directory Names for External Files • Attaching Streams to External Files

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

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

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

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

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

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

  8. The file may have a different format • For example: In grades2.txt Alice 85 Bob 78 Cathy 89 Douglas 92 Eve 100

  9. 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; }

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

  11. 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 }

  12. StringStream #define inFile "InData.txt" #define outFile "OutData.txt" ofstream outs; //output stream string aString; … outs.open(outFile); … outs << '*' << aString << '*';

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

  14. 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/

More Related