1 / 11

Lecture 9 F iles Handling

CS 128 Introduction to C++. Lecture 9 F iles Handling. Sampath Jayarathna Cal Poly Pomona Based on slides created by Bjarne Stroustrup & Tony Gaddis. Using Files for Data Storage. Can use files instead of keyboard, monitor screen for program input, output

sylvia
Download Presentation

Lecture 9 F iles Handling

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. CS 128 Introduction to C++ Lecture 9Files Handling Sampath Jayarathna Cal Poly Pomona Based on slides created by Bjarne Stroustrup & Tony Gaddis

  2. Using Files for Data Storage • Can use files instead of keyboard, monitor screen for program input, output • Allows data to be retained between program runs • Steps: • Open the file • Use the file (read from, write to, or both) • Close the file

  3. File Operations • File: a set of data stored on a computer, often on a disk drive • Programs can read from, write to files • Used in many applications: • Word processing • Databases • Spreadsheets • Compilers

  4. Files: What is Needed • Use fstream header file for file access • File stream types: • ofstream: Stream class to write on files • ifstream: Stream class to read from files • fstream: Stream class to both read and write from/to files. • Define file stream objects: ifstreaminfile; ofstreamoutfile; • Can use >>to read from, and << write to a file • Can use eof member function to test for end of input file

  5. Writing to a new file #include <iostream> #include <fstream> using namespace std; intmain () { ofstreammyfile; myfile.open("example.dat"); // or .txt myfile<< "Writing this to a file.\n"; myfile.close(); return 0; }

  6. Testing for File Open Errors • Can test a file stream object to detect if an open operation failed: ifstreaminfile; infile.open("test.txt"); if (!infile) { cout << "File open failure!"; } • Can also use the fail member function

  7. Using Files Writing with is_open() method // writing on a text file #include <iostream> #include <fstream> using namespace std; void main () { ofstreammyfile ("example.txt"); if (myfile.is_open()) { myfile<< "This is a line.\n"; myfile<< "This is another line.\n"; myfile.close(); } else cout << "Unable to open file"; }

  8. Activity 23 Create a program to write information to an external file called “bio.txt” There should a writeToFile() that takes information to write to a file as parameters. Use getUserInput() to create data for 3 users taken from user input. The format should be exactly **** Profile **** Name: <your name> Age: <DOB> City: <city>

  9. Using Files reading with is_open() method // Reading a text file string line; ifstreammyfile("example.txt"); if (myfile.is_open()) { while( getline(myfile,line) ) { cout<< line << '\n'; } myfile.close(); } else cout << "Unable to open file";

  10. Closing Files • Use the close member function: infile.close(); outfile.close(); • Don’t wait for operating system to close files at program end: • may be limit on number of open files • may be buffered output data waiting to send to file

  11. Activity 24 Create a program that generate 500 random numbers and write them to an external file. Your function name should be generateRandoms() Use int val = rand() % 100 + 1; to generate a number between 1-100. Include <ctime> and srand(time(NULL)); before your rand() call to initialize the random seed values. Read the files in another function called getRandoms() and display the results.

More Related