國立暨南國際大學 101 學年度第二學期小考試卷                   (考試時間: 14:10-14:20)
科目名稱:程式設計 開課系所:資訊工程 學系 任課教師
吳坤熹
系所別:
年級:
學號:
姓名:
考試日期
2013.3.19
  1. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // String Input
    #include <iostream>
    #include <fstream>
    #include <string>
    using std::ofstream;
    using std::ifstream;
    using std::string;

    int main()
    {
        string str;
        ofstream fsOut;
        fsOut.open("fname.txt");
        // connects stream fsOut to the external file "fname.txt".
        fsOut << "Bruce Willis\n";      //Behaves just like cout
        fsOut.close();
        ifstream fsIn("fname.txt");
        fsIn >> str;  
        std::cout << str.length() << std::endl;
        return 0;
    }

  2. (10%) Assume that a file "fname.txt" was generated in the previous question. Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // File Input
    #include <iostream>
    #include <fstream>
    #include <string>
    using std::ifstream;
    using std::string;

    int main()
    {
        string str;
        ifstream fsIn("fname.txt");   
        // The constuctor will open a file "fname.txt"

        fsIn.open();
        fsIn >> str;
        std::cout << str << std::endl;
        fsIn.close();
        return 0;
    }