C++ 在类中使用 fstream getline() 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9711977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using the fstream getline() function inside a class
提问by rocklandcitizen
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.
我正在尝试将包含字典单词的文本文件行加载到数组对象中。我想要一个数组来保存所有以“a”开头的单词,另一个以“b”开头......对于字母表中的所有字母。
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.
我想这需要我重载 getline 函数,但我不太确定如何去做或为什么有必要。
Any advice?
有什么建议吗?
回答by 111111
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 的流函数不是 istream 的成员函数,而是像这样使用的自由函数。(成员函数版本处理 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.
最后尝试不加入using namespace std
那是不好的,原因有很多我不会说;而是用 std:: 前缀 std 对象,例如 std::cout 或 std::string。
http://en.cppreference.com/w/cpp/container/vectorhttp://en.cppreference.com/w/cpp/string/basic_string/getlinehttp://en.cppreference.com/w/cpp/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/细绳