为什么我得到了getline()的C版本? [英] why am I getting the C version of getline()?

查看:64
本文介绍了为什么我得到了getline()的C版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎遇到了getline()的问题。我试图在

google上找到它

,但我没有看到答案。这部分

代码

的目标只是根据

条件从文件读到一个字符串。做... while声明。


/////代码


void populate(vector< string>& unplayed_anagrams,string&单词,const

ifstream& dict_word_list)

{

const int max_size = word.size()+ 1;

int letters_left;

char word_array [max_size];

char test_word_array [max_size];

string measure_word =" " ;;

vector< intused_letters;

vector< int> :: iterator iter;


used_letters.clear();

memset(test_word_array,''\'',max_size);

memset(word_array,''\'',max_size);

strcpy(word_array,word.c_str());


cout<< 请等待......填充数组。 <<结束;


while(!dict_word_list.eof())

{

letters_left =(max_size - 1);

do

{

getline(dict_word_list,measure_word); //< -This是

违规代码

}

while((measure_word.size()word.size()& & measure_word.size()<

3)|| measure_word == word);


\\\\\\\


我有另外一段代码可以做几乎相同的事情,



例外它可以工作。


/////代码


void load_words(字符串& word,ifstream& dict_file)

{

vector< stringtemp_vector;

int random_num = 0;

int temp_size = 0;

string line =" " ;;


temp_vector.clear();


while(word.size()< 6)

{

while(!dict_file.eof())

{

getline(dict_file,line);

temp_vector.push_back(line);

++ temp_size;

}

random_num =(rand()%temp_size);

word = temp_vector [random_num];

}

}


\\\\\ \\\code


当我编译代码时,我得到一个编译时错误,指向第一块代码中的

getline()。 br />

/////错误


jaag.cc:在函数''void

populate(std: :vector< std :: basic_string< char,

std :: char_traits< char> ;,std :: allocator< char,

std :: allocator< std :: basic_string< ; char,std :: char_traits< char>,

std :: allocator< char&,std :: string&,const std :: ifstream&)'':

jaag.cc:126:错误:invali d从''void *''转换为''char **''

jaag.cc:126:错误:无法将''std :: string''转换为''size_t *'' for

参数''2''''__ssize_t getline(char **,size_t *,FILE *)''


\\\\ \\\\\ror错误


我使用的包括:


/////代码


#include< iostream>

#include< vector>

#include< cstdlib>

#include< cctype>

#include< ctime>

#include< fstream>

#include< string>


\\\\\\\


尽我所能,但我在

第一个,C ++版本在第二个。我不能说



out。这不是我需要的。我希望他们都是C ++。任何帮助

这个

将不胜感激。


谢谢


PS我在amd64-gentoo-linux上使用gcc-4.1.2。如果您需要,我可以

发布

调用这些函数的代码。再次感谢。

解决方案

th ************* @ gmail.com 写道:


我好像有getline()的一个问题。我试图在

google上找到它

,但我没有看到答案。这部分

代码

的目标只是根据

条件从文件读到一个字符串。做... while声明。


/////代码


void populate(vector< string>& unplayed_anagrams,string&单词,const

ifstream& dict_word_list)



这是错误,删除const


void populate(vector< string>& unplayed_anagrams,string& word,

ifstream& dict_word_list)


当然在* * *你的函数中,你应该有istream not

ifstream。在函数中使用istream意味着可以使用任何输入流的函数
,即ifstream,fstream,istringstream,

stringstream,cin,以及你要定义的任何其他人。为什么限制你的

功能才能读取文件?


john


谢谢 - 它有效。我可以发誓我试过了。 *咂自我

头*。不管怎样,谢谢。至于功能 - 我不再使用

了。 做一件事,做得好从我所理解的
。如果我再次需要它,我应该更多地了解它并且

它不应该太难修改。


< blockquote> th ************* @ gmail.com 写道:


谢谢 - 它有效。我可以发誓我试过了。 *咂自我

头*。不管怎样,谢谢。至于功能 - 我不再使用

了。 做一件事,做得好从我所理解的
。如果我再次需要它,我应该更多地了解它并且

它不应该太难修改。



LOL你要做的就是从ifstream删除''f',不是太难吗?

这是一个很好的习惯,进入,表明你理解其中一个关键

OO编程点。


john


I seem to be having a problem with getline(). I have tried to find it
on
google, but I''m not seeing the answer. The goal of this section of
code
is simply to read in a line from a file to a string according to the
conditions of the do...while statement.

/////code

void populate( vector<string>& unplayed_anagrams, string& word, const
ifstream& dict_word_list )
{
const int max_size = word.size() + 1;
int letters_left;
char word_array[max_size];
char test_word_array[max_size];
string measure_word = " ";
vector<intused_letters;
vector<int>::iterator iter;

used_letters.clear();
memset( test_word_array, ''\0'', max_size);
memset( word_array, ''\0'', max_size);
strcpy( word_array, word.c_str() );

cout << "Please wait... Populating the arrays." << endl;

while( !dict_word_list.eof() )
{
letters_left = (max_size - 1 ) ;
do
{
getline( dict_word_list, measure_word ); //<-This is the
offending code
}
while((measure_word.size() word.size() && measure_word.size() <
3) || measure_word == word);

\\\\\code

I have another section of code that does almost the same thing, with
the
exception that it works.

/////code

void load_words( string& word, ifstream& dict_file )
{
vector<stringtemp_vector;
int random_num = 0;
int temp_size = 0;
string line = " ";

temp_vector.clear();

while( word.size() < 6 )
{
while( !dict_file.eof() )
{
getline( dict_file, line );
temp_vector.push_back( line );
++temp_size;
}
random_num = ( rand() % temp_size );
word = temp_vector[random_num];
}
}

\\\\\code

When I compile the code, I get a compile time error pointing to the
getline() in the first chunk of code.

/////error

jaag.cc: In function ''void
populate(std::vector<std::basic_string<char,
std::char_traits<char>, std::allocator<char,
std::allocator<std::basic_string<char, std::char_traits<char>,
std::allocator<char &, std::string&, const std::ifstream&)'':
jaag.cc:126: error: invalid conversion from ''void*'' to ''char**''
jaag.cc:126: error: cannot convert ''std::string'' to ''size_t*'' for
argument ''2'' to ''__ssize_t getline(char**, size_t*, FILE*)''

\\\\\error

I was using these includes:

/////code

#include <iostream>
#include <vector>
#include <cstdlib>
#include <cctype>
#include <ctime>
#include <fstream>
#include <string>

\\\\\code

as best as I can figure it I am getting the C version of getline() in
the first one and the C++ version in the second one. I can''t figure
it
out. That''s not what I need. I want them both to be C++. Any help on
this
would be greatly appreciated.

Thanks

PS I am using gcc-4.1.2 on amd64-gentoo-linux. If you need, I can
post
the code that is calling these functions. Thanks again.

解决方案

th*************@gmail.com wrote:

I seem to be having a problem with getline(). I have tried to find it
on
google, but I''m not seeing the answer. The goal of this section of
code
is simply to read in a line from a file to a string according to the
conditions of the do...while statement.

/////code

void populate( vector<string>& unplayed_anagrams, string& word, const
ifstream& dict_word_list )

Here is the error, drop the const

void populate( vector<string>& unplayed_anagrams, string& word,
ifstream& dict_word_list )

And of course in *both* your functions, you should have istream not
ifstream. Using istream in the function means the function can be used
with any input stream, i.e. ifstream, fstream, istringstream,
stringstream, cin, plus any others you care to define. Why limit your
function to reading files only?

john


Thank you - it worked. I could have swore I tried that. *smacks self
in head*. Anyway, thanks. As for the function - I have no use for
anymore functionality. "Do one thing and do it well" from what I
understand. If I ever need it again, I should know more about it and
it shouldn''t be too hard to modify.


th*************@gmail.com wrote:

Thank you - it worked. I could have swore I tried that. *smacks self
in head*. Anyway, thanks. As for the function - I have no use for
anymore functionality. "Do one thing and do it well" from what I
understand. If I ever need it again, I should know more about it and
it shouldn''t be too hard to modify.

LOL, all you have to do is delete the ''f'' from ifstream, not too hard?
And it''s a good habit to get into, shows you understand one of the key
points of OO programming.

john


这篇关于为什么我得到了getline()的C版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆