以下是我目前的代码。
我需要做的是从两个不同的文本文件 Matrix A 和 Matrix B 中读取数据。
我可以做到这一点,但是每次读取一个文本文件矩阵时,只会得到如下结果:
1 0 0
所以基本上第一行是指,矩阵A的整个文本文件实际上是
1 0 0
2 0 0
3 0 0
有人知道我该如何做到这一点吗?
谢谢!
#include <iostream> //declaring variables
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
string code(string& line);
int main()
{
ofstream outf;
ifstream myfile;
string infile;
string line;
string outfile;
cout << "Please enter an input file (A.txt) for Matrix A or (B.txt) for Matrix B" << endl;
cin >> infile; //prompts user for input file
if (infile == "A.txt")
{ //read whats in it and write to screen
myfile.open("A.txt");
cout << endl;
getline (myfile, line);
cout << line << endl;
}
else
if (infile == "B.txt")
{
myfile.open("B.txt");
cout << endl;
getline (myfile, line);
cout << line << endl;
}
else
{
cout << "Unable to open file." << endl;
}
//{
//while("Choose next operation");
//}
return 0;
}