如何在列中追加数据

How do I append data in columns?

本文关键字:追加 数据      更新时间:2023-10-16

下面的代码将 y[i] 的值附加到与 x[i] 相同的列中,y[0] 从 x[10] 下面开始。我想将 y[i] 的值附加到 x[i] 值的第一列旁边的第二列中。你能帮我怎么做吗?

#include <fstream>
using namespace std;
int main() {
  ofstream outfile;
  double x[10], y[10];
  for(int i=0; i<10; i++){
    x[i] = i+10;
  }
  for(int i=0; i<10; i++){
    y[i] = i-10;
  }
  outfile.open("result.txt", ios_base::app);
  outfile << "loop: " << 1 << endl;
  for(int i=0; i<10; i++){
  outfile << x[i] << "n";
  }
  outfile << "loop: " << 2 << endl;
  for(int i=0; i<10; i++){
    outfile << y[i] << "n";
  }
  outfile.close();
return 0;
}

更改

outfile << "loop: " << 1 << endl;
 for(int i=0; i<10; i++){
   outfile << x[i] << "n";
 }

自-

outfile << "loop: 1 t loop: 2"  << endl;
 for(int i=0; i<10; i++){
outfile << x[i] << " t "<<y[i]<<"n";
}

无需

outfile << "loop: " << 2 << endl;
  for(int i=0; i<10; i++){
  outfile << y[i] << "n";}

你想这样做吗?

#include <fstream>
using namespace std;
int main() {
  ofstream outfile;
  double x[10], y[10];
  for(int i=0; i<10; i++){
    x[i] = i+10;
    y[i] = i-10;
  }
  outfile.open("result.txt", ios_base::app);
  outfile << "loop: " << 1 << endl;
  for(int i=0; i<10; i++){
      outfile << x[i] << ' ' << y[i] << "n";
  }
  outfile.close();
  return 0;
}