Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将输出打印到屏幕或给定文件名(c+;+;)的选项_C++_File - Fatal编程技术网

C++ 将输出打印到屏幕或给定文件名(c+;+;)的选项

C++ 将输出打印到屏幕或给定文件名(c+;+;)的选项,c++,file,C++,File,我希望选项2将输出传递给键入的文件名。但是,程序正在创建文件,但没有将输出传递给文件。我认为在这里使用ostream是可以的,但我不知道该怎么做 void displayTable(int n, char op) { int printOption; string outputFileName; ofstream createOutputFile; while (true) { //option for print screen or print to file

我希望选项2将输出传递给键入的文件名。但是,程序正在创建文件,但没有将输出传递给文件。我认为在这里使用ostream是可以的,但我不知道该怎么做

void displayTable(int n, char op) {
    int printOption;
    string outputFileName;
    ofstream createOutputFile;

    while (true) { //option for print screen or print to file
        cout << "Select: \n1) Print on Screen \n2) Print to a file name \nSelection: ";
        cin >> printOption;

        if (printOption == 1)
            break;
        else if (printOption == 2){
            cout << "Type in the name for the output file." << endl;
            cin >> outputFileName;
            createOutputFile.open(outputFileName);
            break;
        }
        else
            cout << "Please enter a valid number." << endl;
    }

    int max = getMaxSize(n, op);
    cout << setw(max) << op << "|";
    for (int i = 1; i <= n; ++i) {
        cout << setw(max) << i;
    }
    cout << endl;
    for (int i = 0; i < max; ++i) {
        cout << "-";
    }
    cout << "+";
    for (int i = 0; i < n * max; ++i) {
        cout << "-";
    }
    cout << endl;
    for (int i = 1; i <= n; ++i) {
        cout << setw(max) << i << "|";
        for (int j = 1; j <= n; ++j) {
            cout << setw(max) << getValue(i, j, op);
        }
        cout << endl;
    }

    cout << endl;
    createOutputFile.close();
}
void显示表(int n,char op){
int打印选项;
字符串输出文件名;
流的createOutputFile;
while(true){//用于打印屏幕或打印到文件的选项
cout>打印选项;
如果(打印选项==1)
打破
else if(printOption==2){
cout输出文件名;
createOutputFile.open(outputFileName);
打破
}
其他的

cout您没有将任何内容打印到
createOutputFile
,而是将所有内容打印到
cout
。这就是为什么在文件中看不到任何内容,在控制台中看不到所有内容的原因

解决问题的最简单方法是将
cout
重定向到
createOutputFile
的输出缓冲区,例如:

auto cout_buff=cout.rdbuf();
...
createOutputFile.open(outputFileName);
cout.rdbuf(createOutputFile.rdbuf())
//所有cout输出现在都将转到文件。。。
...
cout.rdbuf(cout_buff);//完成后恢复。。。
否则,将打印逻辑移动到一个单独的函数,该函数将
ostream&
作为参数:

void域逻辑(ostream&os)
{
//根据需要打印到操作系统。。。
}
...
如果(打印选项==1){
区域性(cout);
打破
} 
如果(打印选项==2){
...
流的createOutputFile(outputFileName);
域逻辑(createOutputFile);
打破
}
...

此问题中显示的代码不符合stackoverflow.com对a的要求,因此,这里的任何人都不可能最终确定问题,最多只能猜测。必须对此问题进行编辑,以显示一个最小的示例,不超过一到两页的代码(“最小”部分),任何人都可以完全按照所示剪切/粘贴、编译、运行和再现所描述的问题(“可再现”部分)(包括任何辅助信息,如程序输入)。有关更多信息,请参阅。