Top Banner
STARTING OUT WITH Class 15 Class 15 Honors Honors
35

STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

Dec 17, 2015

Download

Documents

Abel Owen
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

STARTING OUT WITH

STARTING OUT WITH

Class 15 Class 15 HonorsHonors

Page 2: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

2

• Understand some common

file I/O functions

• Set up a program for writing

to a file

• Read information from a file

Obj

ecti

ves

Obj

ecti

ves

Page 3: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

3

3 Steps to Use a File3 Steps to Use a File

• The file must be opened. If the file does not exist, opening it means creating it.

• Information is then saved to the file, read from the file, or both.

• When the program is finished using the file, the file must be closed.

P. 701

Page 4: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

4

File Name andFile Name and

Extension File ContentsMYPROG.BAS BASIC ProgramMENU.BAT DOS Batch FileINSTALL.DOC Documentation FileCRUNCH.EXE Executable FileGAMES.GRP Windows Group FileBOB.HTML HTML3DMODEL. JAVA java prog. or appletINVENT.OBJ Object FilePROG1.PRJ Borland C++ Porj. FileANSI.SYS Sys. Device DriverREADME.TXT Text FileCLIENTS.DAT

P. 706Table 12-4

Page 5: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

5

Writing Information to a FileWriting Information to a FileFile:

In MemoryVariables:

X

Y

Z

10 25 40

10

25

40Data is copied from variablesinto the file

Page 6: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

6

Reading Information from a FileReading Information from a FileFile:

In MemoryVariables:

X

Y

Z

10 25 40

10

25

40Data is copied from the fileinto variables

Page 7: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

7

File StreamFile StreamData Typeofstream

DescriptionOutput File Stream. This data type can be

used to create files and write information to them. With the ofstream data type, information may only be copied from variables to the file, but not vice-versa.

P. 700Table 12-1

Page 8: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

8

File StreamFile Stream

Data Typeifstream

DescriptionInput File Stream. This data type can be

used to read information from files into memory. With the ifstream data type, information may only be copied from the file into variables, not but vice-versa.

P. 700Table 12-1

Page 9: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

9

File StreamFile Stream

Data Typefstream

DescriptionFile Stream. This data type can be used

to create files, write information to them, and read information from them. With the fstream data type, information may be copied from variables into a file, or from a file into variables.

P. 700Table 12-1

Page 10: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

10

ofstream outClientFile(“clients.dat”, ios::out);

if (!outClientFile) {cerr<<“File could not be opened”

<< endl;exit (1); // prototype in stdlib

}

Page 11: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

11

ofstream outclientFile(“clients.dat”, ios::out);

ofstream outclientFile;

outclientFile.open(“clients.dat”, ios::out);

Page 12: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

12

Opening Modes for Files

• ios::app- Write all output to the end of the file

• ios::ate- Move to the end of the original file when file is opened. Enable data to be written anywhere in the file Table 12- 2

p. 701

Page 13: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

13

Opening Modes for Files (Cont’d)

• ios::in- Open a file for input.

• ios::out- Open a file for output.

• ios::trunc- Discard the file’s contents if it exists (this is also the default action for ios::out) Table 12- 2

p. 701

Page 14: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

14

Opening Modes for Files (Cont’d)

• ios::nocreate- If the file does not exist, the open operation fails.

• ios::noreplace- If the file exists, the open operation fails.

• ios::binary- Information read or written in pure binary format

Page 15: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

15

#include <fstream> using namespace std;int main(){ fstream DataFile;

char FileName[81];cout << “Enter the name of the

file you wish to open\n”;cout << “or create:”;cin.getline(FileName);DataFile.open(FileName, ios::out);cout << “The file ” << FileName <<

”was opened.\n”;}

P. 702Pgrm. 12-1

Page 16: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

16

#include <fstream> using namespace std;int main(){ fstream DataFile;

char FileName[81];cout << “Enter the name of the

file you wish to open\n”;cout << “or create:”;cin.getline(FileName);DataFile.open(FileName, ios::out);cout << “The file ” << FileName <<

”was opened.\n”;}

Page 17: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

17

#include <fstream.h> void main(void){ fstream DataFile;

DataFile.open(“demofile.txt”, ios::out);if (DataFile == 0){ cout << “File open error!” ,, endl;

return; }cout << “File opened successfully.\n”;cout << “Now writing information to the file.\n”;DataFile << “Jones\n”;DataFile << “Smith\n”;DataFile <<“Willis\n”;DataFile << “Davis\n”;DataFile.close( ); }

P. 702Pgrm. 12-1

Page 18: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

18

Program Screen OutputProgram Screen OutputFile opened successfully

Now writing information to the file.

Done.

Output to File DEMOFILE.TXTJonesSmithWilisDavis

P. 702

Page 19: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

19

J o n e s \n S m i t h \n W i l

l i s \n D a v i s \n <EOF>

P. 702

Page 20: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

20

#include <fstream> int main(){ fstream DataFile;

DataFile.open(“demofile.txt”, ios::out);DataFile << “Jones\n”;DataFile << “Smith\n”;DataFile.close( );DataFile.open(“demofile.txt”, ios::app);DataFile << “Willis\n”;DataFile << “Davis\n”;DataFile.close( );

}P. 703Pgrm. 12-2

Page 21: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

21

Output to File DEMOFILE.TXTJonesSmithWilisDavis

Pg. 703

Page 22: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

22

J o n e s \n S m i t h \n W i l

l i s \n D a v i s \n <EOF>

P. 704Fig. 12- 3

Page 23: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

23

//This program writes three rows of//This program writes three rows of//numbers to a file.//numbers to a file.

#include <fstream> # include <iomanip>#include <iostream>using namespace std;int main (){ fstream OutFile(“table.txt, ios::out);

int Nums[3] [3] = { 2897, 5, 837, 34, 7, 1623,

390, 3456, 12 };

P. 709Pgrm. 12-4

Page 24: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

24

//Write three rows of numbers//Write three rows of numbersfor (int Row = 0; row < 3; Row++){

for (int Col = 0; Col < 3; Col++) { OutFile << setw(4)<< Nums[Row] [Col]<<“ ”; } OutFile << endl;

}OutFile.close( );

}P. 709Pgrm. 12-4

Page 25: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

25

Contents of File TABLE.TXT

2897 5 837 34 7 1623 390 3456 12

Pg. 709

Page 26: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

26

#include <fstream>#include <iostream> using namespace std;bool openFileIn(fstream &, char [51]);void showContents(fstream &);int main(){ fstream dataFile;

if (!openFileIn(dataFile,”demofile.txt);{ cout << “File open error!<< endl;return 0; }

P. 710-711Pgrm. 12-5

Page 27: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

27

cout << “File opened successfully.\n”;cout << “Now reading information

from the file.\n\n”; showContents(dataFile); cout << “done” ; return 0; // end of main}bool openFileIn(fstream &file, char *name){ bool status; file.open(name,ios::in); if (file.fail()) status = false; else status = true; return status; }

P. 711Pgrm. 12-5

Page 28: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

28

void ShowContents(fstream &File){ char Name [81];

while (!File.eof( )) {File >> Name;cout << Name << endl;

}

P. 711Pgrm. 12-5

Page 29: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

29

J a y n e M u r p h y \n 4 7

2 \n <EOF>

J o n e s C i r c l e \n A

l m o n d , N C 2 8 7 0

P. 712

Page 30: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

30

#include <fstream> #include <iostream>using namespace std;int main(){ fstream NameFile;

char Input[81];NameFile.open(“murphy.txt”, ios::in);if (NameFile == 0){

cout << “File open error!” << endl;return 0;

} nameFile.getline(input,81);

while (!NameFile.eof( )){ cout << Input;

nameFile.getline(input,81);}NameFile.close( );

}

P. 717Pgrm. 12-8

Page 31: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

31

ExerciseExercise

• Write a program that will read all the data in a file and write all the data to a new file removing the blanks.Modify page 730

Page 32: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

32

#include <fstream> #include <iostream> int main(){ fstream inFile;

ostream outFile(“out.dat”, ios:: out);char Ch, FileName [51];cout << “Enter the name of the file”;cin >> FileName;inFile.open(FileName, ios::in);if (!inFile){ cout << FileName << “could not”

<< “be opened.\n”;return 0;}

P. 723-724Pgrm. 12-12

Page 33: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

33

// prime read inFile.get(ch); while (!inFile.eof( ))

{ if(Ch! = ‘ ‘ ) // change from textbook outFile.put(Ch);

inFile.get(Ch);}File.close( );

return 0;}

P. 723-724Pgrm. 12-12

Page 34: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

34

Q & A

Page 35: STARTING OUT WITH STARTING OUT WITH Class 15 Honors.

STARTING OUT WITH

STARTING OUT WITH

Conclusion of Conclusion of Class 15Class 15