如何使用ifstream打开文件并继续阅读直到结束 [英] How to open a file using ifstream and keep reading it until the end
本文介绍了如何使用ifstream打开文件并继续阅读直到结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是想打开一个名为"test.txt"的文件,其中包含:
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);
该行不是在创建std::ifstream
的实例,而是在声明一个函数.
That line is not creating an instance of std::ifstream
, it is declaring a function.
您需要的行是:
ifstream my_infile(filename);
这篇关于如何使用ifstream打开文件并继续阅读直到结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文