Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.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++ 用setw对齐文本_C++_Setw - Fatal编程技术网

C++ 用setw对齐文本

C++ 用setw对齐文本,c++,setw,C++,Setw,我想像这样证明输出文本的合理性 0x29823d80 0x10019b8 / 0 00000000000000000000000000000001 0x37449e60 0x10dfc / 12 00000000000000000001000000000000 然而,这一声明 fout << std::setw(5) << std::hex << "0x" << (*it).add

我想像这样证明输出文本的合理性

 0x29823d80    0x10019b8         /    0 00000000000000000000000000000001
 0x37449e60    0x10dfc           /   12 00000000000000000001000000000000
然而,这一声明

  fout << std::setw(5) << std::hex << "0x" << (*it).addr << " " 
       << std::setw(5) << std::hex << "0x" << (*it).pc << std::setw(10) << "/" << std::setw(5) << std::dec << (*it).off << "\t" 
       << std::setw(5) << (*it).layout << "\n";

我知道这可能不是您想要的,但请记住,
C
主要是
C++
的一个子集,而且您尤其具有可用的函数fprintf,它比
C++
I/O工具更适合格式化简单的字符串和数字。有了这个,您只需写下:

fprintf(file, "%10p %10p / %5d %d\n", it->addr, it->pc, it->off, it->layout);

我知道这可能不是您想要的,但请记住,
C
主要是
C++
的一个子集,而且您尤其具有可用的函数fprintf,它比
C++
I/O工具更适合格式化简单的字符串和数字。有了这个,您只需写下:

fprintf(file, "%10p %10p / %5d %d\n", it->addr, it->pc, it->off, it->layout);
发件人:

此值不是“粘性的”:受流宽度字段值影响的下一个输入或输出操作将其重置为零(表示“未指定”)

这意味着您执行的
setw
是针对字符串
“0x”
,而不是实际的十六进制数。您必须在要验证的输出之前使用
setw

编辑:解决问题的一个方法是使用包含前导
“0x”
的临时字符串,而不是使用这些字符串的格式:

std::ostringstream val1, val2;

val1 << "0x" << std::hex << (*it).addr;
val2 << "0x" << std::hex << (*it).pc;

fout << val1.str() << " " << val2.str()
     << std::setw(10 + 10 - val2.str().length())
     << '/' ...
std::ostringstream val1,val2;
val1来自:

此值不是“粘性的”:受流宽度字段值影响的下一个输入或输出操作将其重置为零(表示“未指定”)

这意味着您执行的
setw
是针对字符串
“0x”
,而不是实际的十六进制数。您必须在要验证的输出之前使用
setw

编辑:解决问题的一个方法是使用包含前导
“0x”
的临时字符串,而不是使用这些字符串的格式:

std::ostringstream val1, val2;

val1 << "0x" << std::hex << (*it).addr;
val2 << "0x" << std::hex << (*it).pc;

fout << val1.str() << " " << val2.str()
     << std::setw(10 + 10 - val2.str().length())
     << '/' ...
std::ostringstream val1,val2;

val1如有疑问,请使用更多
setw
!此外,您还可以使用
setfill
使数字看起来更漂亮:

std::cout << "0x"
          << std::hex << std::setfill('0') << std::setw(10) << (*it).addr
          << std::setw(5) << std::setfill(' ') << "0x"
          << std::hex << std::setfill('0') << std::setw(10) << (*it).pc
          << std::setw(10) << std::setfill(' ') << "/"
          << std::dec << std::setw(5) << (*it).off
          << std::setw(33) << (*it).layout
          << std::endl;

如有疑问,请使用更多
setw
!此外,您还可以使用
setfill
使数字看起来更漂亮:

std::cout << "0x"
          << std::hex << std::setfill('0') << std::setw(10) << (*it).addr
          << std::setw(5) << std::setfill(' ') << "0x"
          << std::hex << std::setfill('0') << std::setw(10) << (*it).pc
          << std::setw(10) << std::setfill(' ') << "/"
          << std::dec << std::setw(5) << (*it).off
          << std::setw(33) << (*it).layout
          << std::endl;

但是我正在寻找
非常好的解决方案。但总的来说,最好记住,你有完整的
C
工具箱可用,并且务实地使用最适合任务的工具箱,而不是坚持只使用
C++
的子集。但我正在寻找
的解决方案。但总的来说,最好记住,你有完整的
C
工具箱可用,并且要务实地使用最适合任务的工具箱,而不是坚持只使用
C++
的子集。但这可能会导致
0x
与数字分开。我的意思是,使用
setw(5)
我们有
0x12345
0x 345
@mahmood,然后您还需要根据十六进制数的位数调整填充。请看我修改过的答案。但这可能会导致
0x
与数字分开。我的意思是,使用
setw(5)
我们有
0x12345
0x 345
@mahmood,然后您还需要根据十六进制数的位数调整填充。请看我修改过的答案。