Advertisement
Guest User

Untitled

a guest
May 23rd, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include <fstream>
  2. #include <sstream>
  3. #include <iostream>
  4. #include <windows.h>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. string DiretorioDaAplicacao(){
  10.     char buffer[MAX_PATH];
  11.     GetModuleFileName(NULL, buffer, MAX_PATH);
  12.     int pos = string(buffer).find_last_of("\\/");
  13.     return string(buffer).substr(0, pos);
  14. }
  15.  
  16. string LerAquivo(string arquivo){
  17.     ifstream ifs;
  18.     ifs.exceptions (ifstream::failbit | ifstream::badbit);
  19.     try {
  20.         ifs.open(arquivo.c_str());
  21.         stringstream ss;
  22.         ss << ifs.rdbuf();
  23.         string str = ss.str();
  24.         return str;
  25.     }
  26.     catch (ifstream::failure e) {
  27.         /* Fazer alguma coisa aqui caso ocorram erros */
  28.         return "";
  29.     }
  30. }
  31.  
  32. vector<string> ProcurarArquivos(string Diretorio){
  33.     vector<string> arquivos;
  34.     string Busca = Diretorio + "\\*.txt";
  35.     HANDLE hFind;
  36.     WIN32_FIND_DATA data;
  37.     hFind = FindFirstFile(Busca.c_str(), &data);
  38.     if (hFind != INVALID_HANDLE_VALUE){
  39.         do{
  40.             arquivos.push_back(data.cFileName);
  41.         } while (FindNextFile(hFind, &data));
  42.     FindClose(hFind);
  43.     }
  44.     return arquivos;
  45. }
  46.  
  47. int main()
  48. {
  49.     string Diretorio = DiretorioDaAplicacao();
  50.     vector<string> arquivos = ProcurarArquivos(Diretorio);
  51.     for (vector<string>::iterator i = arquivos.begin(); i != arquivos.end(); i++) {
  52.             string conteudo = LerAquivo(*i);
  53.             cout << conteudo << endl;
  54.     }
  55.     cin.get();
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement