COSC 1315

Programming Fundamentals

 Practice Text #200

Simple File I/O

Revised: February 5, 2007
By Richard G. Baldwin

File Pfsg00200.htm
Practice Text Index


Welcome

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.

Questions



200-1.  True or False.  You must include the file named fstream in order to perform file I/O in C++.

Answer and Explanation

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

Answer and Explanation

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?

Listing 200-3a.
-1 1 3 5

Listing 200-3b.
/*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

Answer and Explanation

200-4.  What result is produced by running the program shown in Listing 200-4?

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

Answer and Explanation



Copyright 2007, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

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.

Baldwin@DickBaldwin.com


Answers and Explanations

Answer 4

A.  Compiler Error

Back to Question 4

Explanation 4

The output stream named outFile is declared as the wrong type.  Needs to be type ofstream instead of type ifstream.

Answer 3

E.  -1 1 3

Back to Question 3

Explanation 3

Answer 2

False

Back to Question 2

Explanation 2

The type specifications in the two variable declarations are incorrect.

Answer 1

True

Back to Question 1

Explanation 1



Copyright 2007, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

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.

Baldwin@DickBaldwin.com

-end-