0
#include <iostream>
#include <cstdlib>
#include <fstream>
  //#include "list.cpp"
using namespace std;

int main()
{
    list<string> list;
    ifstream f("./src/main.cpp");
    // Read the file into list
    if (!f.is_open() || !GetLines(f, list).eof())
    {
        cout << "Error reading test2.cpp" << endl;
        return EXIT_FAILURE;
    }
    // Print the contents
    Print(list);
    return 0;
}

istream& GetLines(istream& is, list<string>& list)
{
    // 1. getline(is, list);
    // 2.cin.getline(list, list.end(), '\n');
    // 3. getline(cin, list, '\n');
    return istream&;
}

I need to read lines from a file and write/append them into the list using std::getline function, I've tried different approaches which give me mostly this

error: no instance of overloaded function "getline" matches the argument list -- argument types are: (std::istream, std::__cxx11::list<std::string, std::allocator<std::string>>))

Getlines: Reads all lines from the stream into the specified list. The stored lines should not contain the newline characters. Returns the input stream object after reading all the lines.

param is: The input stream object param list: The list that contains all the lines in the input stream return std::istream& The input stream object after reading all the lines

file main.cpp is main file of my project which has main body in it, which is written above the Getlines function. While Getline function is in another file called list.cpp which is included through #include "list.cpp".

pptaszni
  • 5,591
  • 5
  • 27
  • 43
  • The standard library doesn't contain any `getline()` to read into a list. You can use a `for` loop instead: `for (std::string buffer; std::getline(is, buffer);) list.push_back(buffer);` – Scheff's Cat Oct 06 '22 at 13:14
  • I've to use getline() otherwise assignment will not be graded. can you explain kindly what this for loop does? – Jack Sparrow Oct 06 '22 at 13:15
  • Did you read the [getline](https://en.cppreference.com/w/cpp/string/basic_string/getline) docs? It can take input stream and `std::string` as argument. Why you don't just (in a loop) read the line into the string and then add that string to the list? – pptaszni Oct 06 '22 at 13:17
  • Then you have to write your own overload of `getline()`. I doubt that your prof. or tutor required something which does not exist. You probably got something wrong... – Scheff's Cat Oct 06 '22 at 13:17
  • also these lines confuse me, can you clarify that to me? "The stored lines should not contain the newline characters." – Jack Sparrow Oct 06 '22 at 13:18
  • _can you explain kindly what this for loop does?_ This loop reads a line into a `std::string buffer` and pushes it to the end of `list` or terminates if line reading failed. – Scheff's Cat Oct 06 '22 at 13:18
  • BTW, perfect dupe https://stackoverflow.com/a/7868998/4165552 on how to read file line by line – pptaszni Oct 06 '22 at 13:19
  • yes I read getline() documentation, I'll try to code using string and then appending it to the list, It might help, tysm sir I'm grateful. – Jack Sparrow Oct 06 '22 at 13:20

0 Answers0