1、测试代码
// left2right.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include <iostream>
#include <fstream>
#include<iomanip>
using namespace std;
int main()
{
ofstream ofs;
ofs.open("test.txt", ios::out, ios::binary);
for (size_t i = 0; i < 20; i++)
{
for (size_t j = 0; j < 20; j++)
{
ofs << "|" << std::left << std::setfill(' ') << setw(5) << i << "|" << std::left << std::setfill(' ') << setw(5) << j << "|" << std::left << std::setfill(' ') << setw(10) << "sum" << "|" << std::left << std::setfill(' ') << setw(5) << i + j << std::endl;
cout << "|" << std::left << std::setfill(' ') << setw(5) << i << "|" << std::left << std::setfill(' ') << setw(5) << j << "|" << std::left << std::setfill(' ') << setw(10) << "sum" << "|" << std::left << std::setfill(' ') << setw(5) << i + j << std::endl;
}
}
ofs << "|" << std::left << setw(5) << std::setfill('0') << 100 << "|" << std::left << setw(5) << std::setfill('0') << 56 << "|" << std::left << setw(10) << std::setfill('0') << "testde" << "|" << std::left << setw(5) << std::setfill('0') << 200 << std::endl;
ofs.close();
return 0;
}
2、Console和文本显示对比
3、问题原因和解决办法
此问题由于txt显示字体不是等宽字符造成的,即空格和字符宽度不一致,将文本格式设置为等宽字符即可解决。
4、window下的等宽字体:
BatangChe
Courier
Courier New
DotumChe
Fixedsys
GulimChe
GungsuhChe
Lucida Console
Lucida Sans Typewriter
MingLiU
MS Gothic
MS Mincho
Terminal
仿宋_GB2312
楷体_GB2312
隶书
宋体-方正超大字符集
新宋体
幼圆