2017-02-02 5 views
0

Я пытаюсь прочитать содержимое нескольких файлов с помощью библиотеки Boost. мне удалось перечислить все файлы в папке, но я застрял в попытке прочитать их ...Прочитайте несколько файлов C++ using 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; 
     } 
    } 
} 
+0

Поиск 'fstream' и это отведениях (потоки файлов) ... – Nim

ответ

2

Используйте ifstream, чтобы открыть файл, и getline() прочитать его содержимое в string строке line:

#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"; 
} 
1

Вы можете использовать стандартную библиотеку 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 
    } 
} 
+0

но только читать один файл здесь .... – Boat

+0

@BaptBnnt Вы хотите открыть и прочитать несколько файлов одновременно вместо последовательно? Или даже параллельно? – jotik

+0

@BaptBnnt, это пример. Вы бы поместили такой код внутри своего цикла через 'directory_iterator', чтобы читать каждый файл по очереди. –

0

Для тех, кто хочет весь код.

#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"; 
    } 

} 

}

Смежные вопросы