#G097. GESP level4 文件操作

GESP level4 文件操作

GESP level4 - 文件操作

  1. C++中,关于文件路径说法错误的是() {{ select(1) }}
  • "GESP.txt":指定与当前工作目录中的程序文件相同目录中的GESP.txt文件
  • ".data/GESP.txt":指定与当前工作目录中的程序文件上一级目录下的data目录中的GESP.txt文件
  • "./data/GESP":指定与当前工作目录中的程序文件同级目录下的data目录中的GESP.txt文件
  • "GESP.txt"是绝对路径
  1. 下面哪种方式不能实现将字符串"Happy Spring!"输出重定向到文件log.txt ( ) {{ select(2) }}
  • 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);
  1. 下面哪种方式不能实现将字符串"Welcome to GESP!"输出重定向到文件log.txt ( ) {{ select(3) }}
  • 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);
  1. 小杨用文件重定向实现在log.txt文件中输出日志,则下面横线上应填写( )
    1 #include 
    2 #include 
    3 #include 
    4 using namespace std;
    5 int main()
    6 {
    7 ofstream log_file("log.txt");
    8 streambuf* original_cout = cout.rdbuf();
    9 cout.rdbuf(log_file.rdbuf());
    10 // 在此处填入代码
    11 cout.rdbuf(original_cout); // 恢复原始的标准输出缓冲区
    12 return 0;
    13 }
    {{ 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;
  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(5) }}
  • 10
  • 16
  • 40
  • 24
  1. 如果文件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(6) }}
  • 5
  • 2024
  • 3
  • 0
  1. 执行下列C++代码时输出中的第2行是( )
    int main()
    {
    char s[]={(char)"2024",(char*)"3.16",(char*)"GESP"};
    for (int i = 0; i < 2; i++)
    {
    cout << *s+i << endl;
    }
    cout << endl;
    return 0;
    }
    
    {{ select(7) }}
  • 2024
  • 3.16
  • 024
  • 3
  1. 在C++语言中,通过使用文件重定向操作,可以将程序中输出到cout的内容输出到文件中,这是常用的记录程序运行日志的方法之一。( ) {{ select(8) }}
  • 对
  • 错
  1. 由于文件重定向操作,程序员在使用C++语言编写程序时无法确定通过cout输出的内容是否会被输出到屏幕上。( ) {{ select(9) }}
  • 对
  • 错