1 / 28

INFSY 307 C++ Powerpoint 6 File I/O

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.

madison
Download Presentation

INFSY 307 C++ Powerpoint 6 File I/O

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Stream and File I/OFunction 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: <iostream.h> cin cout cerr

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

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

  7. Part I (Continued) • Insert #include <fstream.h> at the top of the file. • Insert ifstream ifs; in private.

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

  9. 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 <fstream.h>

  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) <stdlib.h> 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 <stdlib.h> 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. 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; }

  24. Function Overloading

  25. //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_

  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 OverloadingExample void sum (int j) { j +=5; cout<<j<<endl; return; } void sum(float j) { j+=5; cout<<j<<endl; return; }

  28. Function Overloadingmain example void main ( j) { void sum (int ); void sum (float); int x = 5; float y = 3.5 sum (x); sum (y); return; }

More Related