C++:テキストファイルに出力
1123 ワード
ファイルからデータを入力するように、データをファイルに出力できます.マトリクスがあると仮定し、結果をテキストファイルに保存します.マトリクスをファイルに出力するコードと、マトリクスを端末に出力するコードは非常に似ていることがわかります.
#include
#include
#include
using namespace std;
int main() {
// create the vector that will be outputted
vector < vector > matrix (5, vector (3, 2));
vector row;
// open a file for outputting the matrix
ofstream outputfile;
outputfile.open ("matrixoutput.txt");
// output the matrix to the file
if (outputfile.is_open()) {
for (int row = 0; row < matrix.size(); row++) {
for (int column = 0; column < matrix[row].size(); column++) {
if (column != matrix[row].size() - 1) {
outputfile << matrix[row][column] << ", ";
}
else {
outputfile << matrix[row][column];
}
}
outputfile << endl;
}
}
outputfile.close();
return 0;
}