gpt4 book ai didi

c++ - 如何在行和列中显示矩阵文件

转载 作者:行者123 更新时间:2023-11-30 03:42:53 24 4
gpt4 key购买 nike

请原谅我的代码格式,因为我刚开始使用 C++。无论如何,我正在尝试从文本文件中读取和显示内容(以矩阵的形式)。

虽然我能够读取文件,但我并没有像在文本文件中那样显示输出,而是在一行中获取所有内容。我不能暴力破解代码,因为我有几个文件的矩阵不同。

例如。矩阵01

9 7 6
8 -1 0

或者matrix02

2 10
3 5
-7 25

下面的代码是我的文件输入,那么我该怎么做才能使它在行和列中而不是在单个行/行中呢?

#include <iostream>
#include <fstream>
using namespace std;

int main(){

ifstream fin; //file input stream
ofstream fout; //file output stream

fin.open("/Desktop/matrix_a.txt");
fout.open("/Desktop/matrix_a.out");
string msg;


while (fin>>msg){//read until eof
cout << msg << " ";
}


fin.close();
fout.close();

}

最佳答案

你说:

I am getting the contents all in a single line.

您的代码执行以下操作:

 while (fin>>msg){//read until eof
cout << "" << msg << " ";
}

这是因为你没有写std::endlstd::cout , 在这里,您不会随时打印换行符。 operator<<不会在语句末尾自动添加换行符。如果需要,这是您的责任。

关于c++ - 如何在行和列中显示矩阵文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36539246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com