Transcript C++

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
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
File I/O
(files are attached to streams)



ifstream - input
ofstream - output
fstream -input and
output
*These need the header file,
<fstream.h>
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.

Part I (Continued)
Insert #include <fstream.h> at the top of the
file.
 Insert ifstream ifs; in private.

How to Proceed
1. Associate file with your program:
#define <filename> “external file”
#define infile “test.dat”
#define outfile “test. out”
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”
How to Proceed
3. Link a type to a stream
ifstream ifs; //associate ifs with the input
// stream
ofstream ofs;//associate ofs with the output
//stream
Remember: #include <fstream.h>
How to Proceed
3. In a function, open the file:
ifs.open (infile);
ofs.open (outfile);
Alternatively, you could combine the definition
and 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
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
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;
}
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
How to Proceed
5. Close your file
ifs.close ();
ofs.close ();
C++ Statements related to file processing
ifs.get (achar);
ofs.put (achar);
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;
}
It is always wise to read everything in as
characters
End of line becomes critical to success
void missapp::read_to_endline(ifstream& ifs)
{
char next;
next=' ';
while ((next != '\n')&&(!ifs.eof()))
{
ifs.get(next);
}
return;
}
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);
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
Sample output_to_report_file ()
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;
}
Function Overloading
//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_
Function Overloading
Function Overloading is the ability to declare functions of the same
name 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
Function Overloading
Example
void sum (int j)
{
j +=5;
cout<<j<<endl;
return;
}
void sum(float j)
{
j+=5;
cout<<j<<endl;
return;
}
Function Overloading
main example
void main ( j)
{
void sum (int );
void sum (float);
int x = 5;
float y = 3.5
sum (x);
sum (y);
return;
}