矩阵 A 的输出与矩阵 B 重叠
Output for the matrix A is overlapped with the Matrix B
所以,这是一个从txt文件中读取矩阵A和B的代码。我发现的以下代码能够读取这两个文件。但是,矩阵 A 的输出与矩阵 B 重叠(下图)
enter image description here
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
,然后你才能考虑从你的文件中读取它.
所以,这是一个从txt文件中读取矩阵A和B的代码。我发现的以下代码能够读取这两个文件。但是,矩阵 A 的输出与矩阵 B 重叠(下图) enter image description here
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
,然后你才能考虑从你的文件中读取它.