2

我正在拼命尝试使用 fstream: 列来生成格式化输出。

有两个函数(无论函数 Func1,Func2)产生输出(写入同一个文件,“example.dat”):

#include <fstream>

int main()
{
    std::ofstream fout;    
    fout.open("example.dat");

    for (int i=0; i<10; i++)
    {
        fout << Func1(i) << std::endl;
    };

    // Func2 depends on Func1, thus them **cannot** be written at the same time: 
    // fout << Func1() << " " << Func2() << std::endl;

    for (int i=0; i<10; i++)
    {
        fout << Func2(i) << std::endl;
    };

    return 0;
}

输出将类似于:

功能1(0)

功能1(1)

.

. .

功能1(9)

功能2(0)

函数2(1)

.

.

.

函数2(9)

我的问题是:如何将此输出生成为两列:

功能 1(0) 功能 2(0)

功能 1(1) 功能 2(1)

.

.

.

虽然它们不是同时写入的。

我怀疑我需要使用 seekp()、tellp(),但不幸的是我不是这方面的大专家。

请帮忙!

先感谢您。

4

5 回答 5

1
vector<ostringstream> streams(10);

for (int i=0; i < 10; ++i)
{
    streams[i] << Func1(i);
}

for (int i=0; i < 10; ++i)
{
    streams[i] << " " << Func2(i);
}

ofstream fout("example.dat");

for (int i=0; i < 10; ++i)
{
    fout << streams[i].str() << endl;
}
于 2012-05-09T12:11:12.247 回答
0

为什么需要 seekp 和 tellp (无论如何,对于粗心的人来说,写入文件中间充满了陷阱)

使用 std::vector (语法可能是错误的),因此:

std::vector<std::string> res1;
std::vector<std::string> res2;
res1.reserve(10);
res2.reserve(10);
for (std::size_t i = 0; i < 10; ++i)
{
    std::stringstream s;
    s << func1(i);
    res1[i] = s.str();
}
//same for res2, func2
for (std::size_t i = 0; i < 10; ++i)
{

    cout << res1[i] << " " << res2[i] << "\n";
}

这可能是错误的格式,需要调整位。std::list 可能会更好。

于 2012-05-09T10:14:37.270 回答
0

假设您确实无法将它们保存在内存中,您可以写入两个文件,回到开头,逐行读取它们并输出到最终文件以创建连接列。

像这样的东西可能会起作用(我没有测试过):

#include <fstream>
#include <string>
int main()
{
    std::fstream  fs_func1;
    std::fstream  fs_func2;
    std::ofstream fout;

    fs_func1.open("func1.dat")
    for (int i=0;i<10;i++)
    {
        fs_func1 << Func1(i) << std::endl;
    }
    fs_func1.seekg(0); // Set get pointer to start of file

    fs_func2.open("func2.dat");
    for (int i=0;i<10;i++)
    {
        fs_func2 << Func2(i) << std::endl;
    };
    fs_func2.seekg(0); // Set get pointer to start of file

    fout.open("example.dat");
    while (!fs_func1.eof() && !fs_func2.eof())
    {
        std::string func1_line;
        std::string func2_line;

        std::getline(fs_func1, func1_line);
        std::getline(fs_func2, func2_line);

        fout << func1_line << " " << func2_line << std::endl;
    }

    return 0;
}

可能有一个 Unix 命令行工具可以一步完成。

于 2012-05-09T11:04:09.107 回答
0

未经测试,但想法是将您的文件视为网格。

const int COLUMN_COUNT = 2;
const int COLUMN_WIDTH = 8;
const int ROW_WIDTH = COLUMN_COUNT * COLUMN_WIDTH + 1; // + 1 is for endl

void write_cell(std::ostream& out, int row, int column, double value) {

  // how many rows is there in the file ?
  out.seekp(0, std::ios_base::end);
  int row_count = out.tellp() / ROW_WIDTH;

  // Do we need to create more rows ?
  for(int i = row_count; i <= row; i++) {
    out.seekp(ROW_WIDTH - 1, std::ios_base::cur);
    out << endl;
  }

  // compute cell position
  int pos = row * ROW_WIDTH + column * COLUMN_WIDTH;

  // move to cell position
  out.seekp(pos);

  // write the cell content
  out << std::setw(COLUMN_WIDTH) << value;
}

int main()
{
  std::ofstream fout;    
  fout.open("example.dat");

  for (int i=0; i<10; i++)
  {
    write_cell(fout, i, 0, Func1(i));
  };

  for (int i=0; i<10; i++)
  {
    write_cell(fout, i, 1, Func2(i));
  };

  return 0;
}
于 2012-05-09T12:33:10.297 回答
-1
for (int i=0;i<10;i++)  
{  
    fout << Func1(i);  
};  
fout<<endl;  

for (int i=0;i<10;i++)  
{  
    fout << Func2(i);  
};  
fout<<endl;  
于 2012-05-09T11:07:49.433 回答