奇怪的初始空 space 将数据写入 c++ 中的文件
Strange initial empty space writing data into a file in c++
我的应用程序遇到了一个奇怪的问题,尽管我尝试了多种解决方案,none 已经解决了我的问题。
我的 c++ 应用程序生成一组包含自定义数据的文件。到目前为止,代码似乎工作正常,文件生成正确,除了第一个。文件的预期格式如下:
@Header
@Line1E1,Line1E2,Line1E3
@Line2E1,Line2E2,Line1E3
但是,我注意到第一个生成的文件(而且只有第一个)的第一行有一个奇怪的空白 space:
@Header
@Line1E1,Line1E2,Line1E3
@Line2E1,Line2E2,Line1E3
由于所有文件的代码都相同,我想知道它出了什么问题,但到目前为止我还没有发现问题的根源。
以下是我的代码的简化版本:
/* Variables */
std::ofstream Fil;
std::stringstream ss;
ss.precision (10);
ss.width (10);
ss.setf (ios::fixed);
for (int Inx = 0; Inx < MaxInx; Inx++) {
sprintf (FilNam, "%i-ExportFile.txt", Inx);
Fil.open(std::string (FilNam).c_str (), std::ofstream::out);
SavSta = Fil.is_open ();
if (SavSta) {
ss << "@Header" << "\n";
ss << "@Matrix name: " << MtxName << "\n";
ss << /* Matrix data goes here */ << "\n";
Fil << ss.rdbuf ();
ss.str(std::string ());
ss.clear ();
}
Fil.close ();
}
它来自:
ss.width(10);
If the standard width of the representation is shorter than the field width, the representation is padded with fill characters
把它去掉就OK了。
我的应用程序遇到了一个奇怪的问题,尽管我尝试了多种解决方案,none 已经解决了我的问题。
我的 c++ 应用程序生成一组包含自定义数据的文件。到目前为止,代码似乎工作正常,文件生成正确,除了第一个。文件的预期格式如下:
@Header
@Line1E1,Line1E2,Line1E3
@Line2E1,Line2E2,Line1E3
但是,我注意到第一个生成的文件(而且只有第一个)的第一行有一个奇怪的空白 space:
@Header
@Line1E1,Line1E2,Line1E3
@Line2E1,Line2E2,Line1E3
由于所有文件的代码都相同,我想知道它出了什么问题,但到目前为止我还没有发现问题的根源。
以下是我的代码的简化版本:
/* Variables */
std::ofstream Fil;
std::stringstream ss;
ss.precision (10);
ss.width (10);
ss.setf (ios::fixed);
for (int Inx = 0; Inx < MaxInx; Inx++) {
sprintf (FilNam, "%i-ExportFile.txt", Inx);
Fil.open(std::string (FilNam).c_str (), std::ofstream::out);
SavSta = Fil.is_open ();
if (SavSta) {
ss << "@Header" << "\n";
ss << "@Matrix name: " << MtxName << "\n";
ss << /* Matrix data goes here */ << "\n";
Fil << ss.rdbuf ();
ss.str(std::string ());
ss.clear ();
}
Fil.close ();
}
它来自:
ss.width(10);
If the standard width of the representation is shorter than the field width, the representation is padded with fill characters
把它去掉就OK了。