2016-07-24 197 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的构造函数,所以我想知道如何编译原始程序。 –