写入输出文件的星形菱形图案C++变得混乱
C++ Star diamond pattern written to output file becomes jumbled
我正在学习C++编码并做简单的初学者练习。目前,我面临一个问题,即我的星形钻石图案输出正确发送到控制台,但文件的相同输出变得混乱。
该程序根据行长度创建一个 * 星形菱形,并尝试将相同的设计保存到名为 output_diamond.txt
的输出文件中。
关于如何处理此类问题的任何提示或解决方案?我知道C++部分非常严格,我确保记录我的代码并提供一个可重新创建的工作示例。
法典
#include <iostream>
#include <string>
#include <fstream>
#include <cassert>
using namespace std;
void pyramid(int rows);
int main() {
pyramid(8);
return 0;
}
/*
* @param int rows
*
* Function to output to console a star pyramid
* based on an amount of rows for a half going
* vertical. If top half of rows is 8, than the
* bottom half will be indetical but reverse with
* a row count of 8.
*/
void pyramid(int rows) {
ofstream fout;
fout.open("output_diamond.txt");
if (fout.is_open()) {
/* Top Half */
// Initialize variable for spaces equal to (rows - 1)
int space = rows - 1;
// Begin first for loop for top half of diamond
for (int i = 1; i <= rows; i++) { // For 1
// Begin for loop for spaces
for (int count = 1; count <= space; count++) { // For 2
// Output to console a single space
cout << ' ';
fout << " ";
} // End For 2
// Begin for loop for star/diamond char symbol
for (int count = 1; count <= (2 * i) - 1; count++) { // For 3
// Output to console a single *
cout << '*';
fout << '*' << endl;
} // End For 3
// Before for loop ends output end of line
cout << endl;
// Decrement -1 to space
space--;
} // End For 1
/* Bottom Half */
// Set value for space variable to 1
space = 1;
// Begin first for loop for bottom half of diamond
for (int i = 1; i <= rows - 1; i++) { // For 1
// Begin loop for spaces
for (int count = 1; count <= space; count++) { // For 2
// Output to console a single space
cout << ' ';
fout << ' ';
} // End For 2
// Begin for loop for star/diamond char symbol
for (int count = 1; count <= (2 * (rows - i)) - 1; count++) { // For 3
// Output to console a single *
cout << '*';
fout << '*' << endl;
} // End For 3
// Before for loop ends output end of line
cout << endl;
// Increment space +1
space++;
} // End For 1
} else {
cout << "Output file did not open!" << endl;
assert(fout);
}
// End
}
控制台输出
*
***
*****
*******
*********
***********
*************
***************
*************
***********
*********
*******
*****
***
*
示例文本文件输出
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
实际的解决方案是采用有效的方法并对其进行推广。 有效的是 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 nn";
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 << "nnUsing ostringstream nn";
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::ostringstream 的实时示例
这是文件的输出:
*
***
*****
*******
*********
***********
*************
***************
*************
***********
*********
*******
*****
***
*
您的cout
行和fout
行应始终成对出现,并且应始终相同。
// Output to console a single *
cout << '*';
fout << '*' << endl;
摆脱<< endl
.这发生在两个地方。
// Before for loop ends output end of line
cout << endl;
添加fout << endl;
。这种情况也发生两次。
相关文章:
- 如何克服提升精神AST混乱
- 整数类型应该显式转换(例如"int"到"无符号")还是只会增加混乱?
- std::result_of语法混乱
- 使用具有默认参数的函数模板进行 decltype 会使结果混乱(一个有趣的问题或 gcc 的错误)
- C++中多线程的混乱
- Arduino LCD 显示屏显示混乱的字母
- 在MSDOS编译器上,用大于最大值的值初始化int时输出混乱
- 库设计混乱.. "public"/"private"(模板)标题,库文件..?
- 常量指针上的混乱
- 智能手机代码厨师问题逻辑混乱
- 数组和指针使算术混乱
- C++智能指针混乱
- 双链表混乱
- SFML视图与窗口混乱
- 如果我将html与QTextEdit一起使用,则键入会很混乱
- 为什么我在有关文件编码格式的QT代码中获得了混乱的代码
- 双结肠混乱
- C 公共变量范围混乱
- 关于返回和参数类型的混乱
- 我的前横向如何混乱