#gesp4w24. 2024GESP四级 文件操作

2024GESP四级 文件操作

2024GESP四级 文件操作

2403真题

1.(2403-11)如果文件 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; //####
    
    cout << endl;
    return 0;
}

{{ select(1) }}

  • 5
  • 2024
  • 3
  • 0

2406真题

2.(2406-6)C++中,关于文件路径说法错误的是()?{{ select(2) }}

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

2409真题

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

#include <iostream>
#include <fstream>
#include <string>
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(3) }}

  • 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;

2412真题

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

{{ select(4) }}

  • 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(oorg_cout);