简体   繁体   中英

How to open a file using ifstream and keep reading it until the end

I simply would like to open a file named "test.txt" that contains:

cat
dog
moose
rabbit

I then want to read from the file and convert its contents into strings for things that I will do later in the program. So far i think what I am doing is going in the right direction. However, I am getting an error and I am unsure what to do. Here is the Error>>>

$ make -f makefile.txt
g++ -g -D HASH_TABLE_SIZE=10 -c hash.cpp
hash.cpp: In member function `void Hash::processFile(std::string)':
hash.cpp:12: error: no match for 'operator>>' in 'my_infile >> word'
makefile.txt:6: recipe for target `hash.o' failed
make: *** [hash.o] Error 1

Here is the code I have so far (it will not compile because of that)

void Hash::processFile(string filename)
{
    string word;
    Hash HashTable;

    ifstream my_infile(const char* filename, ios_base::openmode mode = ios_base::in);
    while(my_infile >> word)//iterate through the file and hash them (handles collisions too)
    {
        //Insert keys and handle collisions
    }

}
ifstream my_infile(const char* filename, ios_base::openmode mode = ios_base::in);

That line is not creating an instance of std::ifstream , it is declaring a function.

You need the line to be:

ifstream my_infile(filename);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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