【问题标题】:Read multiple files C++ using Boost使用 Boost 读取多个文件 C++
【发布时间】:2017-02-02 09:34:01
【问题描述】:

我正在尝试使用 Boost 库读取多个文件的内容。 我已经成功列出了一个文件夹中的所有文件,但我一直在尝试阅读它们......

#include <iostream>
#include "boost/filesystem.hpp"

using namespace std;
using namespace boost::filesystem;

int main(int argc, char* argv[])
{
    // list all files in current directory.
    // You could put any file path in here, e.g. "/home/me/mwah" to list that
    // directory
    path p("/home/baptiste/Bureau");

    directory_iterator end_itr;

    // cycle through the directory
    for (directory_iterator itr(p); itr != end_itr; ++itr)
    {
        // If it's not a directory, list it. If you want to list directories too,
        // just remove this check.
        if (is_regular_file(itr->path())) 
        {
            // assign current file name to current_file and echo it out to the
            // console.
            string current_file = itr->path().string();
            cout << current_file << endl;
        }
    }
}

【问题讨论】:

  • 搜索 fstream 及其衍生(文件流)...

标签: c++ boost


【解决方案1】:

使用ifstream 打开文件,使用getline() 将其内容逐行读入string

#include <fstream>
#include <string>

std::string line;
std::ifstream ifs(itr->path().string().c_str());
if (ifs) {
    while (std::getline(ifs, line)) {
        // Process line
    }
}
else {
    std::cerr << "Couldn't open " << itr->path().string() << " for reading\n";
}

【讨论】:

    【解决方案2】:

    您可以使用 C++ 标准库来读取文件。

    逐行读取文件的最简单方法是

    #include <fstream>
    #include <string>
    
    // ... and put this inside your loop:
    
    if (std::ifstream inFile(current_file.c_str())) {
        std::string line;
        while (std::getline(inFile, line)) {
             // Process line
        }
    }
    

    【讨论】:

    • @BaptBnnt 您想同时打开和读取多个文件,而不是按顺序打开吗?甚至并行?
    • @BaptBnnt,这是一个例子。您可以将这样的代码放在循环中 directory_iterator 上,以依次读取每个文件。
    • 事情是我的文件夹中有多个文件第一个函数列出目录中的所有文件,但我也想打印它们的内容...
    • @MartinBroadhurst 谢谢,我会尽力回复你
    • go一个错误解决不了..."Description Resource Path Location Type no matching function for call to ‘std::basic_ifstream&lt;char&gt;::basic_ifstream(const string&amp;)’ booost.cpp /booost/src line 35 C/C++ Problem "
    【解决方案3】:

    对于那些想要完整代码的人。

    #include <iostream>
    #include "boost/filesystem.hpp"
    #include <fstream>
    #include <string>
    
    
    using namespace std;
    using namespace boost::filesystem;
    
    int main (int argc, char *argv[])
    {
    
    path p ("/home/baptiste/workspace/booost");
    
    directory_iterator end_itr;
    
    
    for (directory_iterator itr(p); itr != end_itr; ++itr)
    {
    
        if (is_regular_file(itr->path())) {
    
            string current_file = itr->path().string();
            cout << current_file << endl; }
    
                                std::string line;
                                std::ifstream ifs(itr->path().string().c_str());
                                if (ifs) {
                                while (std::getline(ifs, line)) {
                            cout<< line;
                        }
        }
        else {
            std::cerr << "Couldn't open " << itr->path().string() << " for reading\n";
        }
    
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-16
      • 2023-03-22
      相关资源
      最近更新 更多