【问题标题】:c++: Using a loop to open filesc++:使用循环打开文件
【发布时间】:2016-07-24 15:51:45
【问题描述】:

我有一个包含 2 个文件夹名称 vector <myClass> vec_fileNames; 的字符串向量,我通过读取包含 2 行的 fileNames.txt 来填充它:

第一

第二

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++ 中完成,还是我打开文件的方式有错误?

【问题讨论】:

  • 为什么你总是使用vec_fileNames[0]?不应该是vec_fileNames[i]吗?
  • 看起来您正在 Windows 上运行它。在这种情况下,您的字符串不会是"C:\Program Folder\\" + fileName + "goalFile.txt" int 吗?
  • 考虑使用基于范围的 for 循环 - 它更具可读性,并且避免了当它真的只需要当前元素时总是访问相同的 [0] 索引等错误。
  • 看你在其他cmets的解释,确定不应该是:ifStream inFile("C:\\Program Folder\\" + fileName + "\\goalFile.txt");。您的代码生成字符串"C:\\Program Folder\\FirstgoalFile.txt" 和"C:\\Program Folder\\SecondgoalFile.txt"。我怀疑这没问题。

标签: c++ file-io ifstream


【解决方案1】:

我创建了与您相同的文件夹结构:

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等行是这两个文件的内容。

【讨论】:

  • 我得到一个“在ifStream inFile("C:\\Program Folder\\" + fileName + "\\goalFile.txt");'std::basic_ifstream&lt;char&gt;::basic_ifstream(std::basic_string&lt;char&gt;)' 的调用没有匹配的函数
  • 与您的代码的唯一区别是我在"goalFile.txt" 之前插入了一个(双)反斜杠,即"\\goalFile.txt"。如果您的原始代码可以编译,为什么不应该编译?
  • @ssskh12:ifstream 没有采用 std::string 的构造函数,AFAIK,所以我想知道您的原始程序是如何编译的。
  • 我将 string path("C:\\Program Folder\\" + fileName + "\\goalFile.txt"); 更改为 string path("C:\\Program Folder\\" + fileName + "\\goalFile.txt").c_str()); 并解决了所有问题。我现在可以打开goalFile.txt。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-07
  • 2014-06-08
相关资源
最近更新 更多