万卷网 > 题目详情
题型:单选题

小杨用文件重定向实现在 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;
}
A.

cout << “This output will go to the log file.” << endl;

B.

log_file << “This output will go to the log file.” << endl;

C.

cout >> “This output will go to the log file.” >> endl;

D.

log_file >> “This output will go to the log file.” >> endl;

更新时间:2025-05-18 11:48:54 |
【知识点】 CCF—GESP C++四级

相似题推荐

判断题

考虑用如下递推方式计算斐波那契数列,时间复杂度是 O(n)。

int n = 10; 
int f[20]; 
f[0] = 0; 
f[1] = 1; 
for (int i = 2; i <= n; i++) 
    f[i] = f[i - 1] + f[i - 2];
A.正确 B.错误
2026-07-22
判断题

小杨正在调试他的温度传感器程序,其中变量 x 保存当前温度。下面这段代码运行后,变量 x 的值变成

了 8 。

int x = 5; 
int *p = &x; 
*p = *p + 3;
A.正确 B.错误
2026-07-22
判断题

下面这段代码实现了选择排序算法。

void sort(int a[], int n) { 
    for (int i = 1; i < n; i++) { 
        int x = a[i]; 
        int j = i - 1; 
        while (j >= 0 && a[j] > x) { 
            a[j + 1] = a[j]; 
            j--; 
        } 
        a[j + 1] = x; 
    } 
}
A.正确 B.错误
2026-07-22
单选题

给定函数 climbStairs(int n) 的定义如下,则 climbStairs(5) 的返回的值是( )。

int climbStairs(int n) { 
    if(n <= 2) return n; 
    int a = 1, b = 2; 
    for(int i = 3; i <= n; i++) { 
        int temp = a + b; 
        a = b; 
        b = temp; 
    } 
    return b; 
}
A.

5

B.

8

C.

13

D.

18

2026-07-22
单选题

执行下面 C++ 程序,会输出( )。

int main() { 
    ofstream fout("test.txt"); 
    fout << "Happy" << endl; 
    fout << "New Year"; 
    fout.close(); 
    ifstream fin("test.txt"); 
    string s1, s2; 
    fin >> s1; 
    getline(fin, s2); 
    fin.close(); 
    cout << s1 << "|" << s2; 
    return 0; 
}
A.Happy|New Year B.Happy| New Year
C.HappyNew Year| D.Happy|
2026-07-22
公众号
客服 反馈
顶部