Using the fstream getline() function inside a class
Asked Answered
J

1

6

I'm trying to load lines of a text file containing dictionary words into an array object. I want an array to hold all the words that start with "a", another one for "b" ... for all the letters in the alphabet.

Here's the class I wrote for the array object.

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

    using namespace std;

    class ArrayObj
    {
    private:

        string *list;
        int size; 

    public:


        ~ArrayObj(){ delete list;}

        void loadArray(string fileName, string letter)
        {
            ifstream myFile;
            string str = "";
            myFile.open(fileName);

            size = 0;

            while(!myFile.eof())
            {
                myFile.getline(str, 100);

                if (str.at(0) == letter.at(0))
                    size++;
            }
            size -= 1; 

            list = new string[size];

            int i = 0;
            while(!myFile.eof())
            {
                myFile.getline(str, 100);

                if(str.at(0) == letter.at(0))
                {
                    list[i] = str;
                    i++;
                }
            }

            myFile.close();
        }


    };

I'm getting an error saying:

2   IntelliSense: no instance of overloaded function     "std::basic_ifstream<_Elem, _Traits>::getline [with _Elem=char, _Traits=std::char_traits<char>]" matches the argument list d:\champlain\spring 2012\algorithms and data structures\weeks 8-10\map2\arrayobj.h  39

I guess it's requiring me to overload the getline function, but I'm not quite certain how to go about or why it's necessary.

Any advice?

Juna answered 14/3, 2012 at 23:37 Comment(2)
"No instance of overloaded function " means that you're calling a function with the wrong arguments. Check to make sure that you're passing the right things to getline - for example, getline seems to need a char**, not a string (which is a char*).Link
@MarshallConover: you're referring to the getline() function in <stdio.h>, not the one in <istream> and <string>.Tabbitha
M
12

the function for streams that deals with std::string is not a member function of istream but rather a free function it is used like so. (the member function version deals with char*).

std::string str;
std::ifstream file("file.dat");
std::getline(file, str);

It is worth noting there are better safer ways to do what you are trying to do like so:

#include <fstream>
#include <string>
#include <vector>

//typedeffing is optional, I would give it a better name
//like vector_str or something more descriptive than ArrayObj
typedef std::vector<std::string> > ArrayObj

ArrayObj load_array(const std::string file_name, char letter)
{
    std::ifstream file(file_name);
    ArrayObj lines;
    std::string str;

    while(std::getline(file, str)){
        if(str.at(0)==letter){
            lines.push_back(str);
        }
    }
    return lines;
}


int main(){
    //loads lines from a file
    ArrayObj awords=load_array("file.dat", 'a');
    ArrayObj bwords=load_array("file.dat", 'b');
    //ao.at(0); //access elements
}

don't reinvent the wheel; checkout vectors they are standard and will save you a lot of time and pain.

Final try not to put in using namespace std that is bad for a whole host of reasons I wont go into; instead prefix std objects with std:: so like std::cout or std::string.

http://en.cppreference.com/w/cpp/container/vector http://en.cppreference.com/w/cpp/string/basic_string/getline http://en.cppreference.com/w/cpp/string

Meyerbeer answered 14/3, 2012 at 23:42 Comment(2)
In your first sentence, I think you might have meant getline() is not a member of istream. However, there is a istream::getline() function. Also note that the behavior is not the same. The member version only reads up to n characters even if it doesn't find the line end, while the non-member version grows the buffer as necessary to read a whole line. In most applications however, the global version, as used in your post, is the desired behavior.Tabbitha
@AndréCaron It is worded badly but it is correctly, the version whcich deals with std::strings... Also if you read the description of his problem you will see that my version is fitting for his domain rather than having a number of characters.Meyerbeer

© 2022 - 2024 — McMap. All rights reserved.