2016-07-24 56 views
-2

我有2個文件夾的名稱vector <myClass> vec_fileNames;這是我從其中包含2線fileNames.txt閱讀充滿字符串矢量:C++:使用循環打開文件

首先

ifstream inFile("c:/file names.txt"); 

if(!inFile) 
{ 
    cout << "File Not Found!\n"; 
    inFile.close(); 
} 

else 
{ 

    string line; 
    myClass class; 


    while (getline(inFile, line)) 
    { 
     class.setFileName(line); 
     vec_fileNames.push_back(class); 
    } 

所以,在這一點上我vec_fileName[0].getFileName =第一個和vec_fileName[1].getFileName =第二

現在我想打開的文件夾內的文件誰的名字都在矢量在一個循環中,所以我這樣做:

for(int i = 0; i < vec_fileNames.size(); i++) 

    { 

     string fileName = vec_fileNames[i].getFileName(); 

     ifstream inFile("C:/Program Folder\\" + fileName + "goalFile.txt"); 

     if(!inFile) 
     { 
      cout << "File Not Found!\n"; 
      inFile.close(); 
     } 

     else 
     { 
      while (getline(inFile, line)) 
      { 
       //do something 
      } 
    } 

到目前爲止,一切都只是沒有被打開的文件好。這甚至可以用C++來完成,或者在打開文件時出現錯誤嗎?

+5

爲什麼你總是使用'vec_fileNames [0]'?它不應該是'vec_fileNames [i]'? – MikeCAT

+0

看起來像你在Windows上運行這個。你的字符串不會是'「C:\ Program Folder \\」+ fileName +「goalFile.txt」'int case? –

+0

考慮使用基於循環的範圍 - 它更具可讀性,避免了像真的只需要當前元素時總是訪問相同的[[0]'索引的錯誤。 –

回答

0

我創建你有相同的文件夾結構:

C:\ 
    Program Folder 
     First 
      goalFile.txt 
     Second 
      goalFile.txt 

,並運行下面簡單的代碼。我不會將文件名存儲在類中的節點,而是直接存儲在向量中。

#include <iostream> 
#include <fstream> 
#include <string> 
#include <vector> 

using namespace std; // I'm no fan of this, but you obviously used it. 

void loadFileNames(vector<string>& vec_fileNames) 
{ 
    ifstream inFile("c:\\file names.txt"); 

    if(!inFile.is_open()) 
    { 
     cout << "File Not Found!\n"; 
     return; 
    // inFile.close(); -- no need to close, it is not open! 
    } 
    else 
    { 
     string line; 

     while (getline(inFile, line)) 
     { 
      cout << line << endl; 
      vec_fileNames.push_back(line); 
     } 
    } 
} 

void openFiles(vector<string>& vec_fileNames) 
{ 
    for(int i = 0; i < vec_fileNames.size(); i++) 
    { 
     string fileName = vec_fileNames[i]; 
     string path("C:\\Program Folder\\" + fileName + "\\goalFile.txt"); 

     ifstream inFile(path.c_str()); 

     if(!inFile.is_open()) 
     { 
      cout << "File" << vec_fileNames[i] << "Not Found!" << endl; 
     } 
     else 
     { 
      cout << "opened file in folder " << vec_fileNames[i] << endl << endl; 

      string line; 
      while (getline(inFile, line)) 
      { 
       cout << line << endl; 
      } 
      cout << endl; 
     } 
    } 
} 

int main(int argc, char* argv[]) 
{ 
    vector<string> fileNames; 

    loadFileNames(fileNames); 
    openFiles(fileNames); 

    return 0; 
} 

該工程,並且產生輸出:

First 
Second 
opened file in folder First 

First goal file 1 
First goal file 2 

opened file in folder Second 

Second goalfile 1 
Second goalfile 2 

線條First goal file 1等是兩個文件的內容。

+0

我得到一個「沒有匹配函數調用」std :: basic_ifstream :: basic_ifstream(std: (「C:\\程序文件夾\\」+文件名+「\\ goalFile.txt」);' – ssskh12

+0

與您的代碼唯一區別在於我插入了一個(雙)反斜槓在''goalFile.txt'',即''\\ goalFile.txt''之前'。爲什麼不應該編譯,如果你的原始代碼編譯? –

+0

@ ssskh12:ifstream沒有使用std :: string,AFAIK的構造函數,所以我想知道如何編譯原始程序。 –