Faster way to create tab deliminated text files?

Fak*_*ken 6 c++

我的许多程序输出大量数据供我在Excel上查看.查看所有这些文件的最佳方法是使用制表符分隔文本格式.目前我使用这段代码来完成它:

ofstream output (fileName.c_str());
for (int j = 0; j < dim; j++)
{
    for (int i = 0; i < dim; i++)
        output << arrayPointer[j * dim + i] << " ";
    output << endl;
}
Run Code Online (Sandbox Code Playgroud)

这似乎是一个非常慢的操作,是一种更有效的方式输出像这样的文本文件到硬盘驱动器?

更新:

考虑到这两个建议,新代码如下:

ofstream output (fileName.c_str());
for (int j = 0; j < dim; j++)
{
    for (int i = 0; i < dim; i++)
        output << arrayPointer[j * dim + i] << "\t";
    output << "\n";
}
output.close();
Run Code Online (Sandbox Code Playgroud)

以500KB/s的速度写入HD

但这写入HD为50MB/s

{
    output.open(fileName.c_str(), std::ios::binary | std::ios::out);
    output.write(reinterpret_cast<char*>(arrayPointer), std::streamsize(dim * dim * sizeof(double)));
    output.close();
}
Run Code Online (Sandbox Code Playgroud)

JPv*_*rwe 6

使用C IO,它比C++ IO快得多.我听说编程竞赛中的人们因为使用C++ IO而不是C IO而超时.

#include <cstdio>

FILE* fout = fopen(fileName.c_str(), "w");

for (int j = 0; j < dim; j++) 
{ 
    for (int i = 0; i < dim; i++) 
        fprintf(fout, "%d\t", arrayPointer[j * dim + i]); 
    fprintf(fout, "\n");
} 
fclose(fout);
Run Code Online (Sandbox Code Playgroud)

只需更改%d为正确的类型.