- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 QTextStream 将数据写入文件。它组织得很好(几乎完美),例如:
i t y yy
0 0.0166667 -0.649999 67.6666
1 0.0333333 0.477777 -43.4444
2 0.05 -0.246295 30.6295
3 0.0666666 0.264197 -18.753
4 0.0833333 -0.0483533 14.1687
5 0.1 0.187791 -7.7791
6 0.116667 0.0581394 6.85273
7 0.133333 0.172351 -2.90181
8 0.15 0.123988 3.60121
9 0.166667 0.184008 -0.734136
以上内容由
制作 stream << qSetFieldWidth(5) << i
<< qSetFieldWidth(12) << t
<< qSetFieldWidth(12) << y
<< qSetFieldWidth(12) << yy
<< endl;
但我希望列按点(小数点分隔符)对齐,例如:
0 0.0166667 -0.649999 67.6666
1 0.0333333 0.477777 -43.4444
2 0.05 -0.246295 30.6295
3 0.0666666 0.264197 -18.753
4 0.0833333 -0.0483533 14.1687
5 0.1 0.187791 -7.7791
6 0.116667 0.0581394 6.85273
7 0.133333 0.172351 -2.90181
8 0.15 0.123988 3.60121
9 0.166667 0.184008 -0.734136
我该怎么做?
最佳答案
您必须将数字分成整数部分(右对齐)和小数部分(左对齐)(并从小数表示形式中去除前导零)。
一种更简单的方法是用适合整数表示中数字位数的空格填充输出(也考虑符号)。
未经测试:
// inefficient, but illustrates the concept:
int NumIntDig(double x) {
stringstream s;
s << int(x);
return s.str().size();
}
stream << qSetFieldAlignment(AlignRight) << qSetFieldWidth(5) << i
<< qSetFieldAlignment(AlignLeft) <<
<< qSetFieldWidth(4-NumIntDig(t)) << " "
<< qSetFieldWidth(8+NumIntDig(t)) << t
<< qSetFieldWidth(4-NumIntDig(y)) << " "
<< qSetFieldWidth(8+NumIntDig(t)) << y
<< qSetFieldWidth(4-NumIntDig(yy)) << " "
<< qSetFieldWidth(8+NumIntDig(t)) << yy
<< endl;
关于c++ - QTextStream 按点对齐(小数分隔符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44093193/
我一直在玩一些 c++ hello world 的东西,并认为我会快速获得 qt 字符串。回显和转换字符串很好,但是 endl似乎很特别: #include int main(int, char*[
我想查看读取 QFile 的 QTextStream 的下一个字符,以创建一个高效的标记器。 但是,我没有找到任何令人满意的解决方案。 QFile f("test.txt"); f.open(QIOD
如何在标题中设置一个新的 QTextStream,就像这样 MainClass{ private: QTextStream out; } 然后,在初始化设置文件,而不是 QTextStream
我正在打开一个文件并获取它的 QTextStream。然后我使用 readLine() 逐行读取流。当该行与某个字符串匹配时,我需要将其替换为另一个字符串。我需要的行为是完全替换该行(即,如果该行是“
我正在学习 QtCreator,但在向文件中写入数据时遇到了问题。这是一些代码: char *str; int i = 1; QFile outFile("path/to/file/ou
我只是关注这个主题“How to print to console when using Qt”,以使我的应用程序正常运行。我开始怀疑 - 是否可以为单个 QTextStream 订阅更多输出?例如,
我尝试从同一个文件读取和写入,但出现奇怪的行为。这是示例代码: 主窗口.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include #include
我的问题很简单,但我无法解决... 在我的标题中: QTextStream *in = NULL; 在一种方法中,QTextStream 被初始化: in = new QTextStream(&fil
我在学习 Qt,使用 Qt5.1,我在重音方面有编码问题。我用葡萄牙语编写带有输出的程序,并且总是必须使用重音符号。该问题可以在以下代码中演示: \#include \#include \#inc
我正在为文本文件编写解析器,但 QTextStream 似乎缺少某些内容。我使用的是 Qt 5.4.1,代码是单线程的,如果这很重要的话。 这是方法: const Event* AviLogFile:
我有这几行代码: QFile file("h:/test.txt"); file.open(QFile::ReadOnly | QFile::Text); QTextStream in(&file);
我正在尝试使用 QTextStream 来格式化数字输出。我已将我的问题简化为一个小测试程序来演示我的问题。以下是我的代码: #include #include #include int mai
我的文件名中有空格,但由制表符分隔。我如何使用 QTextStream 一个一个地阅读它们? 正常的方式会被制表符和空格分割(实际上是任何 QChar::isSpace()),这不是我想要的: QSt
我正在使用以下代码将一些文本写入文件: QFile caFile(outputFolder + "file.extension"); caFile.open(QIODevice::WriteOnly
我想在 linux 机器上读取和解析/proc/PID/status 文件的内容,但 QTextStream.atEnd 在开始读取时总是返回 true。编码: QString procDirecto
我正在使用 QTextStream 将数据写入文件。它组织得很好(几乎完美),例如: i t y yy 0 0.016
我使用以下代码查找从 QFile 中读取的字节数。对于某些文件,它给出了正确的文件大小,但对于某些文件,它给出了一个近似于 fileCSV.size()/2 的值。我正在发送两个文件,其中的字符数相同
readLine()后,如何设置光标位置到行首? 使用 seek() 和 pos() 对我不起作用。 这是我的 file.txt 的样子: Object1 Some-name 2 3.40 1.50
我正在使用 QTextStream 写入 QFile,一切正常。我正在尝试创建一些错误检测,因此我尝试删除写入之间的输出文件。 扼杀,Qtextstream的状态继续显示0(无错误),QFile的er
我需要从 foo.txt 中读取字符串: #include #include #include #include int main( int argc, char** argv ) {
我是一名优秀的程序员,十分优秀!