简体   繁体   English

如何使用 getline 从文件读取到数组

[英]How to use getline to read from a file into an array

I could use your help on this problem.我可以在这个问题上使用你的帮助。 I have a file that contains this:我有一个包含此文件的文件:

1x+1y+1z=5
2x+3y+5z=8
4x+0y+5z=2

I must store that into a string.我必须将它存储到一个字符串中。 Once stored the output should be this:一旦存储,输出应该是这样的:

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

This is the code I have, however it is not outputting anything.这是我拥有的代码,但它没有输出任何内容。 Can anybody help me?有谁能够帮助我? It gives me an error on line 19 but I don't know how to fix it.它在第 19 行给了我一个错误,但我不知道如何修复它。 The error states "no matching function for call."错误指出“没有匹配的调用函数”。

|19|error: no matching function for call to 'std::basic_ifstream::getline(std::string [10], int)'| |19|错误:没有匹配的函数调用'std::basic_ifstream::getline(std::string [10], int)'| |22|error: no matching function for call to 'std::basic_istringstream::basic_istringstream(std::string [10])'| |22|错误:没有匹配的函数调用'std::basic_istringstream::basic_istringstream(std::string [10])'|

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

using namespace std;

int main()
{
    ifstream file("matrix.txt");

    if (!file)
    {
        cout << "Could not open input file\n";
        return 1;
    }


    for (string line[10]; file.getline(line, 10); )
    {
        cout << line << '\n';
        istringstream ss(line);

        double a, b, c, d;
        char x, y, z, eq;

        if (ss >> a >> x >> b >> y >> c >> z >> eq >> d)
        {
            if (x == 'x' && y == 'y' && z == 'z' && eq == '=')
            {
                cout << "a = " << a
                     << "  b = " << b
                     << "  c = " << c
                     << "  d = " << d << '\n';
            }
            else
            {
                cout << "parse error 2\n";
            }
        }
        else
        {
            cout << "parse error 1\n";
        }

    }

}

There are a couple of mistakes. 有两个错误。

Mistake no. 错误号 1 1个

You cannot and should not declare string line[10] . 您不能也不应声明string line[10] You also should not use a for loop. 您也不应使用for循环。 Here is a classic implementation of reading string by string from a stream: 这是从流中逐个字符串读取字符串的经典实现:

string line;
while (file >> line) {
  ... // do stuff
}

Mistake no. 错误号 2 2

Your numbers are ints, not doubles, so you should use: 您的数字是整数,而不是双精度数,因此应使用:

int a, b, c, d;

Saving values into an array 将值保存到数组中

First of all, let me say, that there is no good reason to use raw arrays. 首先,让我说,没有充分的理由使用原始数组。 You should always prefer using standard ADTs, such as the std::vector. 您应该始终喜欢使用标准ADT,例如std :: vector。

In this scenario, you should not read the value right into the vector, since the value may be ill-formed. 在这种情况下,您不应将值直接读入向量中,因为该值可能格式错误。

Instead follow this sequence: 而是遵循以下顺序:

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

  if (successful)
    lines.push_back(line);
}

Here is code to read a file and insert into an array.这是读取文件并插入数组的代码。

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM