这是我到目前为止的代码。
我需要做的是从两个不同的文本文件 Matrix A 和 Matrix B 中读取。
我可以这样做,但是对于我读到的每个文本文件矩阵,它只会出现
1 0 0
(所以基本上是第一行)矩阵 A 的整个文本文件实际上是
1 0 0
2 0 0
3 0 0
那么有人知道我该怎么做吗?
Thanks!
#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;
}
Well, getline
显然得到了一行。
您应该逐行读取直到文件末尾,您可以通过以下方式实现这一点:
while (getline(myfile, line))
out << line << endl;
这意味着:当需要从 myfile 获取一行时,将该行写入输出流。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)