将文本文件读入std :: string。 [英] Read text file into std::string.

查看:110
本文介绍了将文本文件读入std :: string。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个函数是否需要在while循环之后调用eof才能正确?


bool read_file(std :: string name,std :: string& s)

{

std :: ifstream in(name.c_str());

if(!in.is_open())

返回false;


char c;

std :: string str;

while(in.get(c ))

str + = c;


if(!in.eof())

返回false;


s = str;

返回true;

}


谢谢。

解决方案

Jason Heyes写道:

这个函数是否需要在while循环后调用eof才能正确?

bool read_file(std :: string name,std :: string& s)
{st / :: ifstream in(name.c_str());
if(!in .is_open())
返回false;

char c;
std :: string str;
while(in.get(c))
str + = c;

if(!in.eof())
返回false;

s = str;
返回true;
}




循环后调用EOF是检查以确保您退出

循环因为您读到文件的末尾。还有一些其他

的原因,get()可能无法读取字符并返回计数

为0.


Jason Heyes写道:

这个函数是否需要在while循环之后调用eof才能正确?


是的。

bool read_file(std :: string name,std :: string& s)

std: :ifstream in(name.c_str());
if(!in.is_open())
返回false;

char c;
std :: string str;
while(in.get(c))
str + = c;


这个循环是_extremely_ slow(或者你想证明C ++的不切实际吗?)。改为使用getline(in,str)。


if(!in.eof())
返回false;

s = str;


s.swap(str);

返回true;
}




>这个函数是否需要在while循环之后调用eof才能正确?


取决于。流可以终止,同时循环多次

的原因,包括坏硬盘或内存不足等等。

如果你想确保它全部读完字符一直结束

的文件,你必须检查它是否退出循环因为

eof。

bool read_file(std :: string name,std :: string& s)
{
std :: string str;


< snip>

s = str;




btw,如果这是你的代码,我建议你做s.swap(str)而不是

作业。


Does this function need to call eof after the while-loop to be correct?

bool read_file(std::string name, std::string &s)
{
std::ifstream in(name.c_str());
if (!in.is_open())
return false;

char c;
std::string str;
while (in.get(c))
str += c;

if (!in.eof())
return false;

s = str;
return true;
}

Thanks.

解决方案

Jason Heyes wrote:

Does this function need to call eof after the while-loop to be correct?

bool read_file(std::string name, std::string &s)
{
std::ifstream in(name.c_str());
if (!in.is_open())
return false;

char c;
std::string str;
while (in.get(c))
str += c;

if (!in.eof())
return false;

s = str;
return true;
}



Calling EOF after the loop here is a check to insure that you exited
the loop because you read to the end of the file. There are a few other
reasons that get() might not be able to read a char and return a count
of 0.


Jason Heyes wrote:

Does this function need to call eof after the while-loop to be correct?
Yes.
bool read_file(std::string name, std::string &s)
{
std::ifstream in(name.c_str());
if (!in.is_open())
return false;

char c;
std::string str;
while (in.get(c))
str += c;
This loop is _extremely_ slow (or do you want to prove the
impracticality of C++?). Use getline (in, str) instead.

if (!in.eof())
return false;

s = str;
s.swap(str);
return true;
}




> Does this function need to call eof after the while-loop to be correct?

Depends. The stream can terminate that while loop for a number of
reasons including a bad harddrive or an out of memory condition, etc.
If you want to make sure it read all the characters all the way to end
of the file, you have to check if it exited the loop because due to
eof.

bool read_file(std::string name, std::string &s)
{
std::string str;
<snip>
s = str;



btw, if this is your code, I suggest you do s.swap(str) instead of the
assignment.


这篇关于将文本文件读入std :: string。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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