如何使用 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);
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- 使用新行和不使用新行读取文件
- 读取文件并输入到矢量中
- 循环后如何继续阅读
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 如何继续读取不同功能中的文件,而不是从头开始再次读取文件?
- 失败后可以继续从流读取吗?
- 如何从文件中读取,然后继续从cin读取?
- 控制台将继续关闭,而不是读取 if 语句中的字符串
- 如何只要用户愿意就继续读取输入
- 如何使用 ifstream 打开文件并继续读取直到最后
- C++流继续读取相同的前 5 行
- 如何从文件中读取,直到读取到一个字符,然后跳过一些字符,再次继续读取
- C++在eof()之后继续读取文件
- 分部分读取文件,在最后读取的部分之后继续
- 结束函数,同时从该点继续读取文件
- 程序不会在输入文件中继续读取它
- 从 arduino 读取到 c++ 程序时继续接收 NULL