Presentation is loading. Please wait.

Presentation is loading. Please wait.

INFSY 307 C++ Powerpoint 6 File I/O. Stream and File I/O Function Overloading.

Similar presentations


Presentation on theme: "INFSY 307 C++ Powerpoint 6 File I/O. Stream and File I/O Function Overloading."— Presentation transcript:

1 INFSY 307 C++ Powerpoint 6 File I/O

2 Stream and File I/O Function Overloading

3 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.

4 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: cin cout cerr

5 File I/O (files are attached to streams) n ifstream - input n ofstream - output n fstream -input and output *These need the header file,

6 Part I n Open a workspace called mathio n Open a file – call it mathio.h n Create a class called mathio and make its base class be missapp.h n Write a function prototype for open_input_file (). Make it void with no arguments.

7 Part I (Continued) n Insert #include at the top of the file. n Insert ifstream ifs; in private.

8 How to Proceed 1. Associate file with your program: #define “external file” #define infile “test.dat” #define outfile “test. out”

9 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”

10 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

11 How to Proceed 3. In a function, open the file: ifs.open (infile); ofs.open (outfile);

12 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

13 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) for EXIT_FAILURE to be recognized 2) exit (1) is also acceptable

14 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; }

15 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 is needed

16 How to Proceed 5. Close your file ifs.close (); ofs.close ();

17 C++ Statements related to file processing ifs.get (achar); ofs.put (achar);

18 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; }

19 It is always wise to read everything in as characters End of line becomes critical to success

20 void missapp::read_to_endline(ifstream& ifs) { char next; next=' '; while ((next != '\n')&&(!ifs.eof())) { ifs.get(next); } return; }

21 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);

22 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

23 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 ()

24 Function Overloading

25 //FILE:missapp.h #ifndef MISSAPP_H_ #define MISSAP_H_ #include 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_

26 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

27 Function Overloading Example void sum (int j) { j +=5; cout<<j<<endl; return; } void sum(float j) { j+=5; cout<<j<<endl; return; }

28 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; }


Download ppt "INFSY 307 C++ Powerpoint 6 File I/O. Stream and File I/O Function Overloading."

Similar presentations


Ads by Google