use*_*870 5 c++ arrays string matrix
这是我到目前为止的代码.
我需要做的是从两个不同的文本文件,矩阵A和矩阵B中读取.
我可以这样做但是对于我读过的每个文本文件矩阵,它只能得到它
1 0 0
Run Code Online (Sandbox Code Playgroud)
(所以基本上是第一行)实际上是Matrix A的整个文本文件
1 0 0
2 0 0
3 0 0
Run Code Online (Sandbox Code Playgroud)
那么有谁知道我怎么做到这一点?
谢谢!
#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;
}
Run Code Online (Sandbox Code Playgroud)
好吧,getline显然得到一条线.
你应该逐行阅读,直到文件结束,你可以用,例如:
while (getline(myfile, line))
out << line << endl;
Run Code Online (Sandbox Code Playgroud)
这意味着:虽然有一行从myfile获取,但将该行写入输出流.
| 归档时间: |
|
| 查看次数: |
32577 次 |
| 最近记录: |