矩阵 A 的输出与矩阵 B 重叠



所以,这是一个从txt文件中读取矩阵A和B的代码。我找到的以下代码能够读取这两个文件。但是,矩阵 A 的输出与矩阵 B 重叠(下图( 在此处输入图像描述

Matrix A -----
1       2       3
4       5       6
7       8       99 
Matrix B -----
0       1       1
2       0       0
2       0       00 

那么,如何避免重叠,请帮忙:)

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
{   
char ch;
const char *fileName="MatrixA.txt";     // FOR MATRIX A
ifstream file;
file.open(fileName,ios::in);
if(!file)
{
cout<<"Error in opening file!!!"<<endl;
return -1; 
}

while (!file.eof()) 
{
file >> noskipws >> ch; 
cout << ch; 
}
file.close();
}
{
char ch;
const char *fileName="MatrixB.txt";     // FOR MATRIX A
ifstream file;
file.open(fileName,ios::in);
if(!file)
{
cout<<"Error in opening file!!!"<<endl;
return -1; 
}

while (!file.eof()) 
{
file >> noskipws >> ch; 
cout << ch; 
}
file.close();
}

return 0;
}

编辑:谢谢大家!修复了它,是的,我知道这不是读取矩阵的代码(抱歉提供错误信息(。我只想让它看起来像一个呵呵,所以再次感谢

您刚刚打印了两个文件的逐字符副本,它们之间没有任何内容。显然"MatrixA.txt"在文件末尾没有换行符。

您可以在第一个文件的输出后添加一个'n'字符。

std::cout << 'n';

在非常真实的意义上,您没有读取两个矩阵,因为您没有使用文件中存在的任何算术值。如果你想这样做,你首先必须在你的程序中提出一些Matrix的表示形式,然后你才能考虑从你的文件中读取它。

最新更新