Revised: February 5, 2007
By Richard G. Baldwin
File Pfsg00200.htm
Practice Text Index
The practice tests in this series were written specifically for the benefit of my students in COSC 1315, Fundamentals of Programming. They consists of questions, answers, and explanations. The questions are based on the material covered in my series of online lecture notes for the course. Each practice test is keyed to a specific lecture. This practice test is keyed to lecture #200 titled Simple File I/O.
200-2. True or False. The first two statements shown in Listing 200-2 declare variables of the classes ifstream and ofstream, which can be used to provide input and output file stream capability respectively. The last two statements in Listing 200-2 associate those two I/O streams with the physical files named File01in.txt and File01out.txt respectively.
Listing 200-2.
inputStream dataFile;//Input file stream outputStream outFile;//Output file stream dataFile.open("File01in.txt");//Input file name outFile.open("File01out.txt");//Output file name |
200-3. Assuming that the current directory contains a file named File01in.txt, which in turn contains the text shown in Listing 200-3a, what output is produced by the program shown in Listing 200-3b?
-1 1 3 5 |
/*File: File01a.cpp Revised 02/05/07 *********************************************/ #include <iostream> #include <fstream> using namespace std; class File01a{ public: static void classMain(){ File01a* ptrToObject = new File01a(); ptrToObject -> doSomething(); }//End classMain function //----------------------------------------// void doSomething(){ ifstream dataFile; dataFile.open("File01in.txt"); const int size = 3; int temp; for(int cnt = 0;cnt < size;cnt = cnt + 1){ dataFile >> temp; cout << temp << " "; }//end for loop cout << endl; dataFile.close(); }//end doSomething function //----------------------------------------// };//End File01a class //------------------------------------------// int main(){ File01a::classMain(); return 0; }//end main |
200-4. What result is produced by running the program shown in Listing 200-4?
/*File: File01b.cpp Revised 02/05/07 *********************************************/ #include <iostream> #include <fstream> using namespace std; class File01b{ public: static void classMain(){ File01b* ptrToObject = new File01b(); ptrToObject -> doSomething(); }//End classMain function //----------------------------------------// void doSomething(){ ifstream outFile; outFile.open("File01out.txt"); const int size = 3; for(int cnt = 0;cnt < size;cnt = cnt + 1){ outFile << cnt - 1 << " "; }//end for loop cout << endl; outFile.close(); }//end doSomething function //----------------------------------------// };//End File01b class //------------------------------------------// int main(){ File01b::classMain(); return 0; }//end main |
Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two. He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.
The output stream named outFile is declared as the wrong type. Needs to be type ofstream instead of type ifstream.
The type specifications in the two variable declarations are incorrect.
Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two. He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.
-end-