如何在列中附加数据?
How do I append data in columns?
以下代码将 y[i] 的值附加到与 x[i] 相同的列中,y[0] 从 x[10] 下方开始。我想在 x[i] 值的第一列旁边的第二列中附加 y[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;
}
您要这样做吗?
#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;
}
改变
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";}
以下代码将 y[i] 的值附加到与 x[i] 相同的列中,y[0] 从 x[10] 下方开始。我想在 x[i] 值的第一列旁边的第二列中附加 y[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;
}
您要这样做吗?
#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;
}
改变
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";}