[英]how to reset the formatting functions in cout using setw and setfill multiple time
How can I use io manipulators like setfill
and setw
repeatedly without wrecking the output that happens below the other what I'm asking is I want to create a text based window not using win32 functions but a made version in command prompt I'm trying to use IO manipulators to create the shape of the window using a character I have selected in my program but what happens is 我如何反复使用诸如
setfill
和setw
类的io机械setfill
,而不会破坏其他我setfill
的输出,我想创建一个基于文本的窗口而不使用win32函数,而是在命令提示符下创建一个版本使用IO机械手使用我在程序中选择的字符来创建窗口的形状,但是会发生什么情况
cout <<setfill(width) << setfill(style)
want to fill the top with x's and the leave the bottom alone and setfill the bottom of the title bar variable 想要用x填充顶部,而只保留底部并设置标题栏变量的底部
cout <<endl<< titleBar << setw(width) << "[_][]][x]" << endl;
#include <iostream>
#include <iomanip>
using namespace std;
class window
{
public:
window(const char * title)
{
windowTitle = title;
}
void createWindow(int width,int height,char windowStyle,const char * titleBar,const char * windowClass)
{
char style = windowStyle;
cout <<setw(width) <<setfill(style) <<setw(0) <<endl;
cout << titleBar << setw(width) << "[_][]][x]" <<endl;
cout <<setw(width) <<setfill(style) <<setw(0) <<endl;
}
const char * getTitle() const { return windowTitle; }
private:
const char * windowTitle;
const char * windowClass;
};
int main ()
{
window myWindow("Windows Programming");
myWindow.createWindow(50,100,'x',myWindow.getTitle(),"windowclass");
system("pause");
return 0;
}
I can't say I fully comprehend the question, but resetting the fill and width could be done as shown: 我不能说我完全理解这个问题,但是可以按照如下所示完成填充和宽度的重置:
void createWindow(int width,int height,char windowStyle,const char * titleBar,const char * windowClass)
{
char style = windowStyle;
char prev_char = cout.fill(style);
cout << setw(width) << "" << endl;
cout.fill(prev_char);
cout << titleBar << setw(width - strlen(titleBar)) << "[_][]][x]" <<endl;
cout << setw(width) << setfill(style) << "" << endl;
}
The main idea is to use the corresponding member functions from cout which return the previous value which you can later use to reset the fill/width. 主要思想是使用cout中的相应成员函数,这些成员函数返回先前的值,您以后可以使用该函数重置填充/宽度。 Running this code would yield the following result:
运行此代码将产生以下结果:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Windows Programming [_][]][x]
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Update: 更新:
I have updated the code so I can give you a better explanation. 我已经更新了代码,以便为您提供更好的解释。
the functions setw and setfill basically modify some of the internal flags/variables of the std::ostream. 函数setw和setfill基本上修改了std :: ostream的一些内部标志/变量。 setfill permanently modifies the fill character, but setw only modifies the width for the next element.
setfill永久修改填充字符,但setw仅修改下一个元素的宽度。 So let's analyse the code.
因此,让我们分析一下代码。
Instead of using setfill, I am calling directly the method from the std::ostream to set the fill value. 我没有使用setfill,而是直接从std :: ostream调用方法来设置填充值。 The main reason I do this is because it returns the previous value it contained so I can restore later:
我这样做的主要原因是因为它返回了它包含的先前值,所以我以后可以恢复:
char prev_char = cout.fill(style);
The next step is to print the "title bar" or however you wan't to call it. 下一步是打印“标题栏”,或者您不想调用它。 To do so I set the width with setw to the parameter provided.
为此,我使用setw将宽度设置为提供的参数。 As I said before the width is only applied to the next printed element, hence the width characters of value style are printed before the empty string:
正如我之前所说的,宽度仅应用于下一个打印的元素,因此,值样式的宽度字符将在空字符串之前打印:
cout << setw(width) << "" << endl;
In the next line we just recover the original fill value, which is very likely just a space: 在下一行中,我们仅恢复原始的填充值,这很可能只是一个空格:
cout.fill(prev_char);
Then we print the titlebar and the buttons, but we apply a new width so that we can fill the space between the titlebar text and the buttons with white spaces: 然后我们打印标题栏和按钮,但是我们应用了一个新的宽度,以便我们可以在标题栏文本和按钮之间用空白填充空间:
cout << titleBar << setw(width - strlen(titleBar)) << "[_][]][x]" <<endl;
Finally, we print another line of x's of size width , but since we already have the original fill character stored in a variable, we can directly use the setfill function: 最后,我们打印另一行x的 宽度为width的x ,但是由于我们已经将原始填充字符存储在变量中,因此我们可以直接使用setfill函数:
cout << setw(width) << setfill(style) << "" << endl;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.