Getline 只返回空白 [英] Getline is returning nothing but blank

查看:50
本文介绍了Getline 只返回空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连续打开多个文件.文件名都存储在一个向量中,该向量被传递给我的函数.这只是一个简单的测试,以确保一切正常.如果它有效,那么我需要将文件中的任何内容推回另一个向量中.

I am trying to open many files in a row. The file names are all stored in a vector, which is passed to my function. This is just a simple test to make sure that everything is working. If it works, then I will need to pushback whatever the file holds into another vector.

void readfile(vector<string> &filename)
{
    string temp;
    ifstream infile(filename[2].c_str());
    getline (infile, temp);
    cout << temp << endl;
}

这只是输出一个空行,尽管文本文件包含一段信息.好久没用File I/O了,有点生疏了.任何帮助表示赞赏.

This is simply outputting a blank line, although the text file holds about a paragraph of information. I have not used File I/O in a while, so I am a little rusty. Any help is appreciated.

你们都帮了大忙,还有一件事,我需要不加句号或空格传递它们.基本上只是一串字符.

You all have been a great help, one more thing, I need to pass them without periods, or spaces. Basically just a string of chars.

推荐答案

OP 的代码没有检查他们的任何文件 IO 是否成功,所以文件可能没有打开,文件可能是空的,读取可能有由于各种原因失败.

OP's code does not check for success of any of their file IO, so the file may not have opened, the file may be empty, and the read may have failed for any number of reasons.

幸运的是,getline 返回输入流,并且流实现了一个非常简洁的运算符 bool,如果流处于错误状态且无法读取,则返回 false.如果无法读取文件,那么temp的内容肯定是无效的,不应使用.

Fortunately, getline returns the input stream and streams implement a really neat operator bool that returns false if the stream is in an error condition and could not be read. If the file could not be read, the contents of temp are most certainly not valid and should not be used.

所以...

void readfile(vector<string> &filename)
{
    string temp;
    ifstream infile(filename[2].c_str());
    if (getline (infile, temp)) //test the read for success
    {
        cout << temp << endl;
    }
    else
    {
        cout << "failed to read file" << endl;
    }    
}

如果 getline 因任何原因失败,包括文件未打开、文件为空、文件损坏且无法读取,流的状态将被标记为错误并在通过 if() 检查时返回 false.

If getline fails for any reason, including file not open, file is empty, and file is corrupt and unreadable, the stream's state will be marked bad and return false when checked by if().

通常此时您应该检查错误的类型,infile.clear() 流以消除错误条件,并捡起碎片,但在这种情况下没有太多观点.如果您无法将文件的开头读入字符串,那么您就会遇到大问题,应该仔细查看文件 filename[2] 的健康状况和内容.

Normally at this point you should check the type of error, infile.clear() the stream to remove the error condition, and pick up the pieces, but in this case there isn't much point. If you can't read the beginning of a file into a string, you got big problems and should take a closer look at the health and contents of file filename[2].

顺便说一下,如果你的编译器是最新的,ifstream 的构造函数会吃一个 std::string 并且 ifstream infile(filename[2]); 将是有效的.

By the way, if your compiler is relatively up to date, ifstream's constructor will eat a std::string and ifstream infile(filename[2]); will be valid.

风格方面,最好将文件名字符串传递给 readfile,而不是向量.这允许您将 readfile 函数重用于向量的多个元素.

Style-wise, you're better off just passing the filename string into readfile, not the vector. This allows you to reuse the readfile function for more than just element 2 of a vector.

void readfile(string & filename)
{
    string temp;
    ifstream infile(filename);
    if (getline (infile, temp)) //test the read for success
    {
        cout << temp << endl;
    }
    else
    {
        cout << "failed to read file " << filename << endl;
    }    
}

然后打电话

readfile(filename[2]);

扩展此功能以满足 OP 的真正目标

Expanding this function to meet OP's true goal

void readfile(string & filename,
              vector<string> & strings)
{
    string temp;
    ifstream infile(filename);
    if (getline (infile, temp)) //test the read for success
    {
        strings.push_back(temp);
        cout << temp << endl;
    }
    else
    {
        cout << "failed to read file " << filename << endl;
    }    
}

并打电话给

vector<string> strings;
...
readfile(filename[2], strings);

这篇关于Getline 只返回空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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