如何在CStdioFile中写入空格?

时间:2016-12-13 13:26:03

标签: c++ mfc

我正在使用CStdioFile并在编写CString之前尝试留下一些空格。 我试过CStdioFile.Seek(iNumOfSpaces, CStdioFile::current)我写的字符串。 问题是,当我在Notepad ++中打开文件时,它会写入NUL而不是白色空格。 如何将白色空间写成白色而不是NUL? 提前致谢

2 个答案:

答案 0 :(得分:5)

你误解了CFile::Seek()方法的作用。它将文件指针绝对或相对地移动到指定位置。它不会添加/修改文件的内容。

相反,您应该使用支持填充的CString::Format()方法。这是一个显示如何使用它的示例:

CString s;  
s.Format(_T("|%-10s|"), _T("Data")); // left-align
s.Format(_T("|%10s|"), _T("Data")); // right-align

结果字符串将如下所示:

|Data      |
|      Data|

这是一个显示如何实现动态(可变长度)填充的示例:

CString s;  
int n = 10;
s.Format(_T("|%-*s|"), n, _T("Data")); // left-align
s.Format(_T("|%*s|"), n, _T("Data")); // right-align

答案 1 :(得分:-2)

1

CString hh="i7mjmhb";
CString h(' ',31);
hh=h+hh;

2

CString hh=L"i7mjmhb";
CString h(L' ',31);
hh=h+hh;

对于文件,不知道向前是不是ascii或unicode东西