C++ 如何使用getline将文件读入数组

C++ 如何使用getline将文件读入数组,c++,arrays,file-io,C++,Arrays,File Io,在这个问题上我需要你的帮助。我有一个包含以下内容的文件: 1x+1y+1z=5 2x+3y+5z=8 4x+0y+5z=2 我必须把它存储到字符串中。存储后,输出应如下所示: 1x+1y+1z=5 a=1 b=1 c=1 d=5 2x+3y+5z=8 a=2 b=3 c=5 d=8 4x+0y+5z=2 a=4 b=0 c=5 d=2 这是我的代码,但是它没有输出任何东西。有人能帮我吗?它在第19行给了我一个错误,但我不知道如何修复它。错误状态为“没有用于调用的匹配函数” |19 |错误:没

在这个问题上我需要你的帮助。我有一个包含以下内容的文件:

1x+1y+1z=5
2x+3y+5z=8
4x+0y+5z=2
我必须把它存储到字符串中。存储后,输出应如下所示:

1x+1y+1z=5
a=1 b=1 c=1 d=5
2x+3y+5z=8
a=2 b=3 c=5 d=8
4x+0y+5z=2
a=4 b=0 c=5 d=2
这是我的代码,但是它没有输出任何东西。有人能帮我吗?它在第19行给了我一个错误,但我不知道如何修复它。错误状态为“没有用于调用的匹配函数”

|19 |错误:没有用于调用的匹配函数 'std::basic_ifstream::getline(std::string[10],int)'| |22 |错误:没有用于调用的匹配函数 'std::basic_istringstream::basic_istringstream(std::string) [10] )'|

#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
ifstream文件(“matrix.txt”);
如果(!文件)
{
cout>x>>b>>y>>c>>z>>eq>>d)
{
如果(x='x'&&y='y'&&z='z'&&eq='
{

有几个错误

第一个错误

您不能也不应该声明
字符串行[10]
。您也不应该使用
for
循环。以下是从流中逐字符串读取字符串的经典实现:

string line;
while (file >> line) {
  ... // do stuff
}
第二个错误

您的数字是整数,而不是双倍数,因此应使用:

int a, b, c, d;
将值保存到数组中

首先,我要说的是,没有充分的理由使用原始数组。您应该始终更喜欢使用标准ADT,例如std::vector

在这种情况下,您不应该将值直接读入向量,因为该值可能格式不正确

相反,请遵循以下顺序:

vector<string> lines;
string line;
while (file >> line) {
  ... // do stuff

  if (successful)
    lines.push_back(line);
}
矢量线;
弦线;
while(文件>>行){
…//做事
如果(成功)
线。推回(线);
}

以下是读取文件并插入数组的代码

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int MAX_LINES = 100;

int main() {
    std::string strArray[MAX_LINES];
    fstream newFile;
    newFile.open("matrix.txt", ios::in); //open a file to perform read operation
    if (newFile.is_open()) {   //checking whether the file is open
        string tp;
        for (int counter = 0; getline(newFile, tp); counter++) { // get new line
            strArray[counter] = tp; // read into string array
            cout << tp << "\n"; //print the string
        }
        newFile.close(); //close the file object.
    }
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
const int MAX_line=100;
int main(){
标准::字符串串[MAX_LINES];
fstream新文件;
newFile.open(“matrix.txt”,ios::in);//打开一个文件以执行读取操作
如果(newFile.is_open()){//检查文件是否打开
字符串tp;
for(int counter=0;getline(newFile,tp);counter++){//get新行
strArray[counter]=tp;//读入字符串数组

cout每当询问“为什么这段代码不起作用”时,请提供编译/运行时出现的所有错误,切勿使用它。
for(字符串行[10];file.getline(行,10);)
我觉得这一行非常可疑。请在代码块中逐字符发布编译器收到的确切错误,而不是重新编写或缩短的代码块。但我想将这一行存储到数组中。@compscistudent,更新了答案以将行添加到向量。问题是我仍然无法使用向量,必须将其存储到数组中y来操纵数字pointers@compscistudent,您确定要使用
string
s数组吗?也许您应该使用
char
s数组?至少在使用指针处理数字方面是有意义的。。。
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int MAX_LINES = 100;

int main() {
    std::string strArray[MAX_LINES];
    fstream newFile;
    newFile.open("matrix.txt", ios::in); //open a file to perform read operation
    if (newFile.is_open()) {   //checking whether the file is open
        string tp;
        for (int counter = 0; getline(newFile, tp); counter++) { // get new line
            strArray[counter] = tp; // read into string array
            cout << tp << "\n"; //print the string
        }
        newFile.close(); //close the file object.
    }
    return 0;
}