Getline 只返回空白

问题描述 投票:0回答:2

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

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

这只是输出一个空行,尽管文本文件包含一段信息。我已经有一段时间没有使用File I/O了,所以我有点生疏了。任何帮助表示赞赏。

编辑:你们都提供了很大的帮助,还有一件事,我需要在没有句号或空格的情况下传递它们。基本上只是一串字符。

c++ file-io vector getline
2个回答
2
投票

OP 的代码不会检查任何文件 IO 是否成功,因此文件可能尚未打开,文件可能为空,并且读取可能因多种原因而失败。

幸运的是,

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

所以...

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。

通常此时您应该检查错误的类型,

infile.clear()
流以消除错误条件,并收拾残局,但在这种情况下没有太多意义。如果您无法将文件的开头读入字符串,那么您就会遇到大问题,应该仔细查看文件的运行状况和内容
filename[2]

顺便说一句,如果你的编译器相对最新,ifstream 的构造函数将吃掉 std::string 并且

ifstream infile(filename[2]);
将有效。

就风格而言,最好将文件名字符串传递到 readfile 中,而不是向量中。这允许您将

readfile
函数重用于向量的多个元素。

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的真正目标

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);

2024年更新

复制省略现在在某些情况下已成为法律,并且在大多数其他典型用例中很常见,因此应该利用它来进一步简化功能

vector<string> 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;
    }    
    return strings; // will take advantage of Named Return Value Optimization 
                    // (NRVO) to eliminate the copy with most modern compilers.
}

并致电

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

1
投票

您的函数假设文件名向量中至少有三个元素,并忽略除第三个(元素 2)之外的所有文件名。

此外,对 getline() 的调用仅读取文件的第 first 行——这可能是一个空行。

我建议您编写一个函数来提取单个文件的内容,并在循环中使用它(在另一个函数中?)来处理文件名列表中的每个文件名。

与此相关,“文件名”意味着单个文件名; “文件名”会更好地表明您的变量是多个文件名。

// Return file content as a single string
string readfile( const string& filename )
{
  ifstream f( filename );
  stringstream ss;
  ss << f.rdbuf();
  return ss.str();
}

// Return file content as a vector/deque/whatever of strings
deque<string> readfile( const string& filename )
{
  ifstream f( filename );
  deque<string> lines;
  string line;
  while (getline( f, line )) lines.emplace_back( line );
  return lines;
}

不报告错误。 (你只会有一个空字符串/向量/双端队列/等等。)

在循环中的其他地方使用它。这里我有一个文件名->文件数据的映射,加载文件名列表中的每个文件名:

for (const string& filename : filenames)
  filedata[ filename ] = readfile( filename );

希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.