Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/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++_Formatting_Fstream_Iostream - Fatal编程技术网

写入输出文件的C++星型金刚石模式变得混乱 我正在学习C++代码,做简单的初学者练习。目前,我面临一个问题,即我的星形菱形图案输出正确地发送到控制台,但发送到文件的相同输出变得混乱

写入输出文件的C++星型金刚石模式变得混乱 我正在学习C++代码,做简单的初学者练习。目前,我面临一个问题,即我的星形菱形图案输出正确地发送到控制台,但发送到文件的相同输出变得混乱,c++,formatting,fstream,iostream,C++,Formatting,Fstream,Iostream,此程序基于行长度创建*星菱形,并尝试将相同的设计保存到名为output_diamond.txt的输出文件中 关于如何处理这样的问题,有什么建议或解决方案吗?我理解C++部分非常严格,我确保了我的代码文档,并提供了一个可重复使用的例子。 代码 文本文件输出示例 您的cout和fout行应该始终成对出现,并且应该始终相同 // Output to console a single * cout << '*'; fout << '*' << endl; 去掉你的c

此程序基于行长度创建*星菱形,并尝试将相同的设计保存到名为output_diamond.txt的输出文件中

关于如何处理这样的问题,有什么建议或解决方案吗?我理解C++部分非常严格,我确保了我的代码文档,并提供了一个可重复使用的例子。 代码

文本文件输出示例

您的cout和fout行应该始终成对出现,并且应该始终相同

// Output to console a single *
cout << '*';
fout << '*' << endl;
去掉你的cout和fout行应该总是成对出现,并且应该总是相同的

// Output to console a single *
cout << '*';
fout << '*' << endl;

摆脱这个实际的解决办法是采取有效的方法并加以推广。工作的是std::cout生成的输出,因此所需的只是编写一个函数,该函数接受一个流,并执行std::cout正在执行的操作

实现这一点的方法是认识到std::ostream是输出流的基类,因此只需使用多态性并编写引用std::ostream的函数

以下是一个例子:

#include <iostream>
#include <string>
#include <ostream>
#include <fstream>
#include <sstream>
#include <cassert>

using namespace std;

void pyramid(int rows, std::ostream& );

int main() 
{
    // use regular cout
    std::cout << "Using cout \n\n";
    pyramid(8, std::cout);

    // try file printing
    std::ofstream fout;
    fout.open("output_diamond.txt");
    pyramid(8, fout);

    // try printing to a string stream
    std::cout << "\n\nUsing ostringstream \n\n";
    std::ostringstream ostrm;
    pyramid(8, ostrm);
    std::cout << "\n";
    std::cout << ostrm.str();
    return 0;
}

void pyramid(int rows, std::ostream& sout) 
{
    int space = rows - 1;
    for (int i = 1; i <= rows; i++) { 
        for (int count = 1; count <= space; count++) { 
            sout << ' ';
        } 
        for (int count = 1; count <= (2 * i) - 1; count++) { 
            sout << '*';
        } 
        sout << endl;
        space--;
    } 
    space = 1;
    for (int i = 1; i <= rows - 1; i++) { 
        for (int count = 1; count <= space; count++) { 
            sout << ' ';
        } 
        for (int count = 1; count <= (2 * (rows - i)) - 1; count++) { 
            sout << '*';
        } 
        sout << endl;
        space++;
    }
}

实际的解决办法是采取有效措施并加以推广。工作的是std::cout生成的输出,因此所需的只是编写一个函数,该函数接受一个流,并执行std::cout正在执行的操作

实现这一点的方法是认识到std::ostream是输出流的基类,因此只需使用多态性并编写引用std::ostream的函数

以下是一个例子:

#include <iostream>
#include <string>
#include <ostream>
#include <fstream>
#include <sstream>
#include <cassert>

using namespace std;

void pyramid(int rows, std::ostream& );

int main() 
{
    // use regular cout
    std::cout << "Using cout \n\n";
    pyramid(8, std::cout);

    // try file printing
    std::ofstream fout;
    fout.open("output_diamond.txt");
    pyramid(8, fout);

    // try printing to a string stream
    std::cout << "\n\nUsing ostringstream \n\n";
    std::ostringstream ostrm;
    pyramid(8, ostrm);
    std::cout << "\n";
    std::cout << ostrm.str();
    return 0;
}

void pyramid(int rows, std::ostream& sout) 
{
    int space = rows - 1;
    for (int i = 1; i <= rows; i++) { 
        for (int count = 1; count <= space; count++) { 
            sout << ' ';
        } 
        for (int count = 1; count <= (2 * i) - 1; count++) { 
            sout << '*';
        } 
        sout << endl;
        space--;
    } 
    space = 1;
    for (int i = 1; i <= rows - 1; i++) { 
        for (int count = 1; count <= space; count++) { 
            sout << ' ';
        } 
        for (int count = 1; count <= (2 * (rows - i)) - 1; count++) { 
            sout << '*';
        } 
        sout << endl;
        space++;
    }
}

你没有打印相同的东西,那么为什么你会期望它们产生相同的结果呢?你在fout中写了很多endl,但是没有cout。你在使用fout,为什么你要同时向标准输出和文件输出不同的东西?你的逻辑很难理解。为了简化工作,为什么不将所有内容写入std::stringstream,然后将其内容打印到标准输出或文件中,而无需重复自己的操作?我建议您将业务逻辑提取并整合到一个函数中,该函数将std::ostream&作为参数,并用std::cout或std::ofstream&instance调用它ῥεῖ 这正是解决这个问题的方法。创建一个接受std::ostream&的函数,将代码写入该ostream,并将流类型作为参数传递。您没有打印相同的内容,为什么希望它们产生相同的结果?你在fout中写了很多endl,但是没有cout。你在使用fout,为什么你要同时向标准输出和文件输出不同的东西?你的逻辑很难理解。为了简化工作,为什么不将所有内容写入std::stringstream,然后将其内容打印到标准输出或文件中,而无需重复自己的操作?我建议您将业务逻辑提取并整合到一个函数中,该函数将std::ostream&作为参数,并用std::cout或std::ofstream&instance调用它ῥεῖ 这正是解决这个问题的方法。创建一个接受std::ostream&的函数,将代码写入该ostream,然后简单地将流类型作为参数传递。非常感谢您,您能这样做真是太好了。因为从字面上说,我只是在建立你建议的方法,并且刚刚开始理解它。很高兴知道我的和你的一样。所以我真的在学习。非常感谢你这么做,你这么做真是太好了。因为从字面上说,我只是在建立你建议的方法,并且刚刚开始理解它。很高兴知道我的和你的一样。所以我实际上在学习。
// Before for loop ends output end of line
cout << endl;
#include <iostream>
#include <string>
#include <ostream>
#include <fstream>
#include <sstream>
#include <cassert>

using namespace std;

void pyramid(int rows, std::ostream& );

int main() 
{
    // use regular cout
    std::cout << "Using cout \n\n";
    pyramid(8, std::cout);

    // try file printing
    std::ofstream fout;
    fout.open("output_diamond.txt");
    pyramid(8, fout);

    // try printing to a string stream
    std::cout << "\n\nUsing ostringstream \n\n";
    std::ostringstream ostrm;
    pyramid(8, ostrm);
    std::cout << "\n";
    std::cout << ostrm.str();
    return 0;
}

void pyramid(int rows, std::ostream& sout) 
{
    int space = rows - 1;
    for (int i = 1; i <= rows; i++) { 
        for (int count = 1; count <= space; count++) { 
            sout << ' ';
        } 
        for (int count = 1; count <= (2 * i) - 1; count++) { 
            sout << '*';
        } 
        sout << endl;
        space--;
    } 
    space = 1;
    for (int i = 1; i <= rows - 1; i++) { 
        for (int count = 1; count <= space; count++) { 
            sout << ' ';
        } 
        for (int count = 1; count <= (2 * (rows - i)) - 1; count++) { 
            sout << '*';
        } 
        sout << endl;
        space++;
    }
}
       *
      ***
     *****
    *******
   *********
  ***********
 *************
***************
 *************
  ***********
   *********
    *******
     *****
      ***
       *