繁体   English   中英

如何使用ifstream打开文件并继续阅读直到结束

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

我只是想打开一个名为“test.txt”的文件,其中包含:

cat
dog
moose
rabbit

然后我想从文件中读取并将其内容转换为字符串,以便我稍后在程序中执行这些操作。 到目前为止,我认为我正在做的是朝着正确的方向前进。 但是,我收到一个错误,我不确定该怎么做。 这是错误>>>

$ 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

这是我到目前为止的代码(由于这个原因,它不会编译)

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);

该行没有创建std::ifstream的实例,它正在声明一个函数。

你需要的是:

ifstream my_infile(filename);

暂无
暂无

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

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