C++ 矩阵A的输出与矩阵B重叠
这是从txt文件中读取矩阵a和B的代码。我发现下面的代码能够读取这两个文件。然而,矩阵A的输出与矩阵B重叠(下图) 那么,如何避免重叠,请帮助:)C++ 矩阵A的输出与矩阵B重叠,c++,fstream,iostream,C++,Fstream,Iostream,这是从txt文件中读取矩阵a和B的代码。我发现下面的代码能够读取这两个文件。然而,矩阵A的输出与矩阵B重叠(下图) 那么,如何避免重叠,请帮助:) #包括 #包括 使用名称空间std; int main() { { char ch; const char*fileName=“MatrixA.txt”;//用于矩阵A ifstream文件; 打开(文件名,ios::in); 如果(!文件) { coutch; cout您刚刚打印了两个文件的逐字符副本,它们之间没有任何内容。显然,“Matr
#包括
#包括
使用名称空间std;
int main()
{
{
char ch;
const char*fileName=“MatrixA.txt”;//用于矩阵A
ifstream文件;
打开(文件名,ios::in);
如果(!文件)
{
coutch;
cout您刚刚打印了两个文件的逐字符副本,它们之间没有任何内容。显然,“MatrixA.txt”在文件末尾没有换行符
您只需在第一个文件的输出后添加一个'\n'
字符即可
std::cout << '\n';
std::cout Usestd::cout Compile with all warning and debug info,因此g++-Wall-Wextra-g
with。改进代码以避免出现警告。使用调试器了解程序的行为。改进并重复,直到满意为止。不要复制粘贴代码(不会修复复制代码的注释),改为创建函数。@Jarod42 std::endl不等于换行符,因为它flushes@Gladaed:对于初学者来说,哪一个更好(在崩溃之前打印先前的(调试)输出,这有助于定位错误)。
#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;
}
std::cout << '\n';