Download presentation
Presentation is loading. Please wait.
Published byAlexander Ellis Modified over 8 years ago
2
Topics 1.File Basics 2.Output Formatting 3.Passing File Stream Objects to Functions 4.More Detailed Error Testing 5.Member Functions for Reading and 6.Writing Binary Files
3
File Basics A file—set of data stored permanently on an external storage device—e.g., hard disk, USB drive, etc. Programs can read from and write to files. File types Binary files / Text files Sequential / Random files Program files / Data files Image files / Audio files / Video files Application files (Word, Excel) / Application Data files (.doc,.docx,.xls, xlsx) 13-3
4
Basic Steps in Using a File 1.Open the file 2.Use (read from, write to) the file 3.Close the file 13-4
5
File Stream Objects Use of files requires file stream objects There are three types of file stream objects (1) ifstream objects: used for input (2) ofstream objects: used for output (3) fstream objects: used for both input and output 13-5 Computer MemoryExternal Storage A B C D E A B C D E F G H I J K L M N O P Q R S T Ifstream infile myfile.dat
6
Opening a File for Input Create an ifstream object in your program ifstream inFile; Open the file by passing its name to the stream’s open member function inFile.open("myfile.dat"); 13-6
7
Getting File Name from User Define file stream object, variable to hold file name ifstream inFile; char fileName[81]; // C-string Prompt user to enter filename and read the filename cout << "Enter filename: "; cin.getline(fileName, 81); Open the file inFile.open(fileName); 13-7
8
Using C++ string instead of C-string Define file stream object, variable to hold file name ifstream inFile; string fileName; Prompt user to enter filename and read the filename cout << "Enter filename: "; getline(cin, fileName); Open the file inFile.open(fileName.c_str()); // Note: string to C-string conversion 13-8
9
Opening a File for Output Create an ofstream object in your program ofstream outFile; Open the file by passing its name to the stream’s open member function outFile.open("myfile.dat"); 13-9
10
Detecting File Open Errors Two methods for detecting if a file open failes (1) Call fail() on the stream inFile.open("myfile"); if (inFile.fail()) { cout << "Can't open file"; exit(1); } 13-10
11
Detecting File Open Errors (2) Test the status of the stream using the ! operator inFile.open("myfile"); if (!inFile) { cout << "Can't open file"; exit(1); } 13-11
12
Using fail() to detect eof Example of reading all integers in a file //attempt a read int x; infile >> x; while (!infile.fail()) { //success, so not eof cout << x; //read again infile >> x; } 13-12
13
Your Turn Write a program which allows the user to input 3 lines of text. The program writes each line to a file. It then reads those text lines from the file and echo prints them to the screen.
14
Passing File Stream Objects to Functions File stream objects keep track of current read or write position in the file Always use pass by reference when passing file stream object to function 13-14
15
Passing File Stream Objects to Functions //print file contents to screen void printFile(ifstream &fin); int main(){ ifstream fin; fin.open(“myfile.dat”); printFile(fin);... } 13-15
16
Passing File Stream Objects to Functions // print file contents to screen void printFile(ifstream &fin){ int x; fin >> x; while(!in.fail()){ cout << x << " "; fin >> x; } } 13-16
17
Passing File Stream Objects //Write to file void writeTo(ofstream &fout, int list[], int count); int main(){ int list[] = {2, 4, 6, 8, 10}; int count = 5; ofstream fout; fout.open(“myfile.dat”); writeTo(fout, list, count);... } 13-17
18
Passing File Stream Objects // Write to file void writeTo(ofstream &fout, int list[], int count); for (int i = 0; i < count; i++){ fout << list[i] << ‘ ‘; } } 13-18
19
Your Turn Convert the preceding “Your Turn” program so that writing data to a file and reading data from the file use two user-defined functions.
21
Binary File Binary Files contain numeric data without conversion to ASCII characters. Storing 1234: in Text File: 1 in Binary File: ‘1’ 0000 001001101 0010 ‘2’‘3’‘4’
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.