+ All Categories
Home > Documents > INFSY 307 C++ Powerpoint 6 File I/O

INFSY 307 C++ Powerpoint 6 File I/O

Date post: 19-Jan-2016
Category:
Upload: madison
View: 18 times
Download: 1 times
Share this document with a friend
Description:
INFSY 307 C++ Powerpoint 6 File I/O. Stream and File I/O Function Overloading. Files. It is much easier to store large quantities of data on a disk. We can review data stored on a disk because it remains there even after the program has ended. External Files. - PowerPoint PPT Presentation
28
INFSY 307 C++ Powerpoint 6 File I/O
Transcript
Page 1: INFSY 307 C++ Powerpoint 6 File I/O

INFSY 307C++

Powerpoint 6File I/O

Page 2: INFSY 307 C++ Powerpoint 6 File I/O

Stream and File I/O

Function Overloading

Page 3: INFSY 307 C++ Powerpoint 6 File I/O

Files

It is much easier to store large quantities of data on a disk.

We can review data stored on a disk because it remains thereeven after the program has ended.

Page 4: INFSY 307 C++ Powerpoint 6 File I/O

External Files

Streams: Interface between program and a device

These are a sequence of characters or bytes and have no fixed length; i.e. a data stream

We have used predefined streams:

<iostream.h> cin cout cerr

Page 5: INFSY 307 C++ Powerpoint 6 File I/O

File I/O

(files are attached to streams)

ifstream - input ofstream - output fstream -input and

output

*These need the header file, <fstream.h>

Page 6: INFSY 307 C++ Powerpoint 6 File I/O

Part I

Open a workspace called mathio Open a file – call it mathio.h Create a class called mathio and make its

base class be missapp.h Write a function prototype for

open_input_file (). Make it void with no arguments.

Page 7: INFSY 307 C++ Powerpoint 6 File I/O

Part I (Continued)

Insert #include <fstream.h> at the top of the file.

Insert ifstream ifs; in private.

Page 8: INFSY 307 C++ Powerpoint 6 File I/O

How to Proceed

1. Associate file with your program:

#define <filename> “external file”

#define infile “test.dat”#define outfile “test. out”

Page 9: INFSY 307 C++ Powerpoint 6 File I/O

2. Or have the user input a file name to associate file with your program:

cin>>infile; cin>>outfile;

infile [ ] is equal to “test.dat”outfile [ ] is equal to “test. out”

Page 10: INFSY 307 C++ Powerpoint 6 File I/O

How to Proceed

3. Link a type to a stream

ifstream ifs; //associate ifs with the input // streamofstream ofs;//associate ofs with the output //stream

Remember: #include <fstream.h>

Page 11: INFSY 307 C++ Powerpoint 6 File I/O

How to Proceed

3. In a function, open the file:

ifs.open (infile);

ofs.open (outfile);

Page 12: INFSY 307 C++ Powerpoint 6 File I/O

Alternatively, you could combine the definitionand open (as per Savitch text)

ifs.open(“math.dat”);

note: in this case, you still must associate ifs with ifstream

ifstream ifs; //associate ifs with the input // stream

Page 13: INFSY 307 C++ Powerpoint 6 File I/O

How to Proceed

4. Check to see that your file opened (closed) successfully

if (ifs.fail()) { cerr<<“**Error on Open: ”<<infile<<endl; cout<<“Press any key to exit program”<<endl; system (“pause”); return EXIT_FAILURE; }

*note: 1) <stdlib.h> for EXIT_FAILURE to be recognized 2) exit (1) is also acceptable

Page 14: INFSY 307 C++ Powerpoint 6 File I/O

int mathio::open_input_file (){ char infile [26]; cout<<“Enter a filename: “<<endl; cin>>infile; ifs.open(infile); if (ifs.fail()) { cerr<<“**Error on Open: ”<<infile<<endl; cout<<“Press any key to exit program”<<endl; system (“pause”); return EXIT_FAILURE; } else return 0;}

Page 15: INFSY 307 C++ Powerpoint 6 File I/O

How to Proceed

EXIT_FAILURE: is symbolic of an unsuccessful open (note: Savitch uses exit (1) to exit program)

EXIT_SUCCESS:is symbolic of a successful open

<stdlib.h> is needed

Page 16: INFSY 307 C++ Powerpoint 6 File I/O

How to Proceed

5. Close your file

ifs.close ();ofs.close ();

Page 17: INFSY 307 C++ Powerpoint 6 File I/O

C++ Statements related to file processing

ifs.get (achar);

ofs.put (achar);

Page 18: INFSY 307 C++ Powerpoint 6 File I/O

void missapp::read_and_clean (int i, ifstream& ifs, double& digit){char next; char digit_str [15]; int n=0, flag=1;ifs.get (next);while (n < i && next != '\n' && !ifs.eof()){ if (isdigit(next)) { digit_str [n] = next; n++;} else { n++; flag=0; } if (n<i) ifs.get (next); } if (flag ==0) digit=-1; else { digit_str [n]= '\0'; digit=(atof(digit_str)) / 100; } return;}

Page 19: INFSY 307 C++ Powerpoint 6 File I/O

It is always wise to read everything in as characters

End of line becomes critical to success

Page 20: INFSY 307 C++ Powerpoint 6 File I/O

void missapp::read_to_endline(ifstream& ifs){

char next;next=' ';while ((next != '\n')&&(!ifs.eof())){ ifs.get(next);} return;

}

Page 21: INFSY 307 C++ Powerpoint 6 File I/O

Report Processing

You may use formatting commands with any output stream

Example:

ofs.setf(ios::fixed); //i.e. no e notation for //floating point numbers ofs.setf(ios::showpoint) ; //show decimal point and //trailing 0’s ifs.precision (2);

Page 22: INFSY 307 C++ Powerpoint 6 File I/O

Formatting I/O

• May use screen style stream operators with file processing commands.• Reports are the best way to use such commands.• There needs to be no validation on output! Therefore, printing one character at a time is not necessary. ofs>>n1<<“ “<<n2<<endl; //note the output file //identifier is used instead //of cout

Page 23: INFSY 307 C++ Powerpoint 6 File I/O

void mathio::output_to_report_file (){ astericks (78); //assume you change astericks //to write a report ofs<<setw (5)<<“”<<setw (25)<<s.first_name<<setw(25)<<s.last_name; ofs <<setw(4)<<letter_grade<<endl; astericks (78); return;}

Sample output_to_report_file ()

Page 24: INFSY 307 C++ Powerpoint 6 File I/O

Function Overloading

Page 25: INFSY 307 C++ Powerpoint 6 File I/O

//FILE:missapp.h#ifndef MISSAPP_H_#define MISSAP_H_#include <iostream.h>#include <fstream.h>#include <stdlib.h>#include <ctype.h>class missapp{ public: void read_and_clean (int, ifstream&, double&); void read_and_clean (int, ifstream&, int&); void read_and_clean (int, ifstream&, char []); void read_to_endline (ifstream&); int read_convert_chars (char thename [], int size); int read_convert_to_int (); private: int flag;}; #endif //MISSAPP_H_

Page 26: INFSY 307 C++ Powerpoint 6 File I/O

Function Overloading

Function Overloading is the ability to declare functions of the samename with different sets of parameters

1) C++ selects the function by examining a) number of arguments b) types of the argument variables c) order of the arguements 2) Used to create several functions with the same name that perform a similar task but with different data types 3) Improves program readability for closely related tasks

Page 27: INFSY 307 C++ Powerpoint 6 File I/O

Function OverloadingExample

void sum (int j){ j +=5; cout<<j<<endl; return;}

void sum(float j){ j+=5; cout<<j<<endl; return;}

Page 28: INFSY 307 C++ Powerpoint 6 File I/O

Function Overloadingmain example

void main ( j){ void sum (int ); void sum (float); int x = 5; float y = 3.5 sum (x); sum (y); return;}


Recommended