Chapter 12 (Streams and Files) - Exercises

¡Supera tus tareas y exámenes ahora con Quizwiz!

Start with the Distance class from the ENGLCON example in Chapter 6, "Objects and Classes." Using a loop similar to that in the DISKFUN example in this chapter, get a number of Distance values from the user, and write them to a disk file. Append them to existing values in the file, if any. When the user signals that no more values will be input, read the file and display all the values.

// ex12_1.cpp // write array #include <iostream> #include <fstream> // for file streams using namespace std; //////////////////////////////////////////////////////////////// class Distance // English Distance class { private: int feet; float inches; public: Distance() : feet(0), inches(0.0) // constructor (no args) { } // constructor (two args) Distance(int ft, float in) : feet(ft), inches(in) { } void getdist() // get length from user { cout << "\n Enter feet: "; cin >> feet; cout << " Enter inches: "; cin >> inches; } void showdist() // display distance { cout << feet << "\'-" << inches << '\"'; } }; //////////////////////////////////////////////////////////////// int main() { char ch; Distance dist; // create a Distance object fstream file; // create input/output file // open it for append file.open("DIST.DAT", ios::binary | ios::app | ios::out | ios::in ); do // data from user to file { cout << "\nDistance"; dist.getdist(); // get a distance // write to file file.write( (char*)&dist, sizeof(dist) ); cout << "Enter another distance (y/n)? "; cin >> ch; } while(ch=='y'); // quit on 'n' file.seekg(0); // reset to start of file // read first distance file.read( (char*)&dist, sizeof(dist) ); int count = 0; while( !file.eof() ) // quit on EOF { cout << "\nDistance " << ++count << ": "; // display dist dist.showdist(); file.read( (char*)&dist, sizeof(dist) ); // read another } // distance cout << endl; return 0; }

Write a program that emulates the DOS COPY command. That is, it should copy the contents of a text file (such as any .CPP file) to another file. Invoke the program with two command-line arguments—the source file and the destination file—like this: C>ocopy srcfile.cpp destfile.cpp In the program, check that the user has typed the correct number of command-line arguments, and that the files specified can be opened.

// ex12_2.cpp // imitates COPY command #include <fstream> //for file functions #include <iostream> using namespace std; #include <process.h> //for exit() int main(int argc, char* argv[] ) { if( argc != 3 ) { cerr << "\nFormat: ocopy srcfile destfile"; exit(-1); } char ch; //character to read ifstream infile; //create file for input infile.open( argv[1] ); //open file if( !infile ) //check for errors { cerr << "\nCan't open " << argv[1]; exit(-1); } ofstream outfile; //create file for output outfile.open( argv[2] ); //open file if( !outfile ) //check for errors { cerr << "\nCan't open " << argv[2]; exit(-1); } while( infile ) //until EOF { infile.get(ch); //read a character outfile.put(ch); //write the character } return 0; }

Write a program that returns the size in bytes of a program entered on the command line: C>filesize program.ext

// ex12_3.cpp // displays size of file #include <fstream> //for file functions #include <iostream> using namespace std; #include <process.h> //for exit() int main(int argc, char* argv[] ) { if( argc != 2 ) { cerr << "\nFormat: filename\n"; exit(-1); } ifstream infile; //create file for input infile.open( argv[1] ); //open file if( !infile ) //check for errors { cerr << "\nCan't open " << argv[1]; exit(-1); } infile.seekg(0, ios::end); //go to end of file // report byte number cout << "Size of " << argv[1] << " is " << infile.tellg(); cout << endl; return 0; }


Conjuntos de estudio relacionados

Ch. 8 Private Payers/Blue Cross Blue Shield

View Set

II Lecture Chapter 21 Short Answer: Lower Leg Surgery pp 470

View Set

Chapter 39 Care of Patients with Shock

View Set

The Neurological System (Part 1)

View Set

Intermediate Accounting II Chapter 15

View Set

Incorrect NCLEX PassPoint Questions

View Set