将矩阵写入txt文件中;每行C ++之后?

时间:2014-03-10 18:52:34

标签: c++ file-io matrix

我有一个大小为24 * 24的矩阵“PowerMatrix”现在我需要知道如何将这个矩阵写入带有的文本文件中;每排后?我怎样才能做到这一点? 我有这个代码到现在,但它不工作,我不知道为什么,但我不能分开使用;每一排之后?

std::ofstream output("Power vector.txt"); 
for (k=1; k<PowerMatrix.size(); k++)
{
for (l=1; l<PowerMatrix.size(); l++)
{
output << PowerMatrix[i][j] << " "; // behaves like cout - cout is also a stream
}
output << "\n";
} 

1 个答案:

答案 0 :(得分:2)

只需在换行符之前添加;

std::ofstream output("Power vector.txt"); 
for (k=1; k<PowerMatrix.size(); k++)
{
    for (l=1; l<PowerMatrix.size(); l++)
    {
        output << PowerMatrix[i][j] << " "; // behaves like cout - cout is also a stream
    }
    output << ";" << endl;
}