C++ 在类中使用fstream getline()函数
我正在尝试将包含字典单词的文本文件的行加载到数组对象中。我想要一个数组来容纳所有以“a”开头的单词,另一个数组代表“b”。。。对于字母表中的所有字母 这是我为数组对象编写的类C++ 在类中使用fstream getline()函数,c++,fstream,getline,C++,Fstream,Getline,我正在尝试将包含字典单词的文本文件的行加载到数组对象中。我想要一个数组来容纳所有以“a”开头的单词,另一个数组代表“b”。。。对于字母表中的所有字母 这是我为数组对象编写的类 #include <iostream> #include <string> #include <fstream> using namespace std; class ArrayObj { private: s
#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();
}
};
#包括
#包括
#包括
使用名称空间std;
类ArrayObj
{
私人:
字符串*列表;
整数大小;
公众:
~ArrayObj(){删除列表;}
void loadArray(字符串文件名、字符串字母)
{
ifstreammyfile;
字符串str=“”;
myFile.open(文件名);
尺寸=0;
而(!myFile.eof())
{
getline(str,100);
if(str.at(0)=字母at(0))
大小++;
}
尺寸-=1;
列表=新字符串[大小];
int i=0;
而(!myFile.eof())
{
getline(str,100);
if(str.at(0)=字母at(0))
{
列表[i]=str;
i++;
}
}
myFile.close();
}
};
我得到一个错误,说:
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
2 IntelliSense:重载函数“std::basic_ifstream::getline[with _Elem=char,_Traits=std::char_Traits]”的实例与参数列表d:\champlain\spring 2012\algorithms and data structures\weeks 8-10\map2\arrayobj.h 39不匹配
我想这需要我重载getline函数,但我不太确定该怎么做,也不确定为什么需要这样做
有什么建议吗?处理std::string的streams函数不是istream的成员函数,而是一个自由函数,它是这样使用的。(成员函数版本处理char*) 值得注意的是,有更好更安全的方法来做你想做的事情:
#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
}
#包括
#包括
#包括
//typedeffing是可选的,我会给它一个更好的名字
//比如vector_str或者比ArrayObj更具描述性的东西
typedef std::vector>ArrayObj
ArrayObj加载数组(常量std::字符串文件名,字符字母)
{
std::ifstream文件(文件名);
ArrayObj线;
std::字符串str;
while(std::getline(文件,str)){
if(str.at(0)=字母){
线。推回(str);
}
}
回流线;
}
int main(){
//从文件中加载行
ArrayObj awords=load_数组(“file.dat”,“a”);
ArrayObj bwords=load_数组(“file.dat”,“b”);
//ao.at(0);//访问元素
}
不要重新发明轮子;它们是标准的,将为您节省大量的时间和痛苦
最后,尽量不要使用名称空间std输入,这是不好的,因为有很多原因我都不想说;相反,用std::作为std对象的前缀,就像std::cout或std::string一样
“没有重载函数的实例”表示调用的函数参数错误。检查以确保您正在向getline传递正确的内容-例如,似乎需要一个字符**,而不是一个字符串(它是一个字符*)。@MarshallConver:您指的是
中的getline()
函数,而不是
和
中的函数。在您的第一句话中,我想您的意思可能是getline()
不是istream
的成员。但是,有一个istream::getline()
函数。还要注意,行为是不一样的。即使找不到行尾,成员版本最多只能读取n
个字符,而非成员版本根据需要增加缓冲区以读取整行。然而,在大多数应用程序中,文章中使用的全局版本是理想的行为。@AndréCaron它的措辞很糟糕,但它是正确的,whcich处理std::String的版本。。。另外,如果你阅读了他的问题描述,你会发现我的版本适合他的领域,而不是有很多字符。
#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
}