#6089. gesp四级真题分类七:文件操作

gesp四级真题分类七:文件操作

七、文件操作(共11题)

1. 下面C++代码执行后生成的文件其字节数为( )。

ofstream fout;
fout.open("1.txt");
for(int i=1;i<=10;i++) {
    if(i%5==0) {
        int x = 6;
        fout << x;
    } else {
        char ch='A';
        fout << ch;
    }
}

{{ select(1) }}

  • 10
  • 16
  • 40
  • 24

2. 如果文件1.txt中的内容如下,则执行下面C++代码时,注释了###那行代码所输出的 x 的值为( )。

文件内容:50 2024 3.16 I love GESP!

int main() {
    ifstream fin;
    string line;
    int x;
    fin.open("1.txt", ios::in);
    for (int i=0; i<2; i++){
        fin >> line;
        cout << line << endl;
    }
    fin >> x;
    cout << x << endl; // ####
}

{{ select(2) }}

  • 5
  • 2024
  • 3
  • 0

3. 在C++中,关于文件路径说法错误的是( )。

{{ select(3) }}

  • "GESP.txt":指定与当前工作目录中的程序文件相同目录中的GESP.txt文件
  • "./data/GESP.txt":指定与当前工作目录中的程序文件上一级目录下的data目录中的GESP.txt文件
  • "./data/GESP.txt":指定与当前工作目录中的程序文件同级目录下的data目录中的GESP.txt文件
  • "GESP.txt"是绝对路径

4. 小杨用文件重定向实现在log.txt文件中输出日志,则下面横线上应填写( )。

#include <iostream>
#include <fstream>
using namespace std;
int main() {
    ofstream log_file("log.txt");
    streambuf* original_cout = cout.rdbuf();
    cout.rdbuf(log_file.rdbuf());
    // 在此处填入代码
    cout.rdbuf(original_cout);
    return 0;
}

{{ select(4) }}

  • cout << "This output will go to the log file." << endl;
  • log_file << "This output will go to the log file." << endl;
  • cout >> "This output will go to the log file." >> endl;
  • log_file >> "This output will go to the log file." >> endl;

5. 下面哪种方式不能实现将字符串"Welcome to GESP!"输出重定向到文件log.txt( )。

{{ select(5) }}

  • freopen("log.txt", "w", stdout); cout << "Welcome to GESP!" << endl; fclose(stdout);
  • std::ofstream outFile("log.txt"); outFile << "Welcome to GESP!" << endl; outFile.close();
  • std::ofstream outFile("log.txt"); cout << "Welcome to GESP!" << endl; outFile.close();
  • ofstream log_file("log.txt"); streambuf* org_cout = cout.rdbuf(); cout.rdbuf(log_file.rdbuf()); cout << "This output will go to the log file." << endl; cout.rdbuf(org_cout);

6. 下面哪种方式不能实现将字符串"Happy Spring!"输出重定向到文件log.txt( )。

{{ select(6) }}

  • freopen("log.txt", "w", stdout); cout << "Happy Spring!" << endl; fclose(stdout);
  • std::ofstream outFile("log.txt"); outFile << "Happy Spring!" << endl; outFile.close();
  • std::ofstream outFile("log.txt"); cout << "Happy Spring!" << endl; outFile.close();
  • ofstream log_file("log.txt"); streambuf* org_cout = cout.rdbuf(); cout.rdbuf(log_file.rdbuf()); cout << "Happy Spring!" << endl; cout.rdbuf(org_cout);

7. 下面哪种方式不能实现将字符串"Welcome to2026!"输出重定向到文件log.txt( )。

{{ select(7) }}

  • freopen("log.txt", "w", stdout); cout << "Welcome to 2026!" << endl; fclose(stdout);
  • std::ofstream outFile("log.txt"); cout << "Welcome to 2026!" << endl; outFile.close();
  • ofstream log_file("log.txt"); streambuf* org_cout = cout.rdbuf(); cout.rdbuf(log_file.rdbuf()); cout << "Welcome to 2026!" << endl; cout.rdbuf(org_cout);
  • std::ofstream outFile("log.txt"); outFile << "Welcome to 2026!" << endl; outFile.close();

8. 文本文件1.txt第1行由01234共5个字符组成其间没有空格,当用C++代码正常打开文件成功并执行如下代码以后,第1行长度为5。(判断题)

{{ select(8) }}

  • 正确
  • 错误

9. 下面的C++代码中,将对1.txt文件写入hello。(判断题)

ifstream filein;
ofstream fileout;
filein.open("1.txt");
fileout << "hello";

{{ select(9) }}

  • 正确
  • 错误

10. 下面C++代码实现将Hello写入data.txt。(判断题)

ofstream out("data.txt");
out << "Hello";
out.close();

{{ select(10) }}

  • 正确
  • 错误

11. 以下代码将Hello写入文件data.txt。(判断题)

ofstream file("data.txt");
cout << "Hello" << endl;
file.close();

{{ select(11) }}

  • 正确
  • 错误