-1

I have a vector of strings of 2 folder names vector <myClass> vec_fileNames; which I filled by reading from a fileNames.txt which contains 2 lines:

First

Second

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);
    }

So, at this point my vec_fileName[0].getFileName = First and vec_fileName[1].getFileName = second

Now I wanted to open files inside the folders who's names are in the vector in a loop so I did this:

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
            }
    }

So far everything is good except for the file not being opened. Is this even something that can be done in c++ or is there an error in the way I'm opening the file?

Community
  • 1
  • 1
ssskh12
  • 23
  • 1
  • 5
  • 5
    Why you are always using `vec_fileNames[0]`? Shouldn't it be `vec_fileNames[i]`? – MikeCAT Jul 24 '16 at 15:52
  • Looks like you are running this on windows. Wouldn't your string be `"C:\Program Folder\\" + fileName + "goalFile.txt"` int that case? – Awais Chishti Jul 24 '16 at 15:57
  • Consider using a range based for loop - it's so much more readable and avoids bugs like always accessing the same `[0]` index when it really just wants the current element. – Jesper Juhl Jul 24 '16 at 15:58
  • @Awais Chishti `/` works just fine as a path separator on Windows. But in any case, using a raw string literal would get rid of all the "having to escape stuff" crazyness. – Jesper Juhl Jul 24 '16 at 15:59
  • @JesperJuhl Ah okay – Awais Chishti Jul 24 '16 at 16:00
  • @FirstStep [`ifstream`'s constructor](http://en.cppreference.com/w/cpp/io/basic_ifstream/basic_ifstream) can open the file too. – Xiobiq Jul 24 '16 at 16:01
  • @Polikdir I did not know that. Thanks for telling me – Khalil Khalaf Jul 24 '16 at 16:05
  • I had vec_fileNames[0] there to test if it was opening because its easier to test with 1 instead everything in the vector – ssskh12 Jul 24 '16 at 16:05
  • 1
    Could you please provide a [MCVE] ? – Khalil Khalaf Jul 24 '16 at 16:07
  • Why don't you assemble the full path of the file before `ifStream inFile(...);` so you can debug if that is actually an existing filename? – Rudy Velthuis Jul 24 '16 at 16:25
  • 1
    Looking at your explanation in other comments, are you sure it should not be: `ifStream inFile("C:\\Program Folder\\" + fileName + "\\goalFile.txt");`. Your code produces the strings `"C:\\Program Folder\\FirstgoalFile.txt"` and `"C:\\Program Folder\\SecondgoalFile.txt"`. I doubt that is OK. – Rudy Velthuis Jul 24 '16 at 16:29
  • I've added more information about my code so you can get a better idea of what I'm working with – ssskh12 Jul 24 '16 at 16:37

1 Answers1

0

I created the same folder structure as you have:

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

And ran the following simple code. Node that I don't store the filenames in a class, but directly into a vector.

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

That works, and produces the output:

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

The lines First goal file 1, etc. are the contents of the two files.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
  • I'm getting an "no matching function for call to `'std::basic_ifstream::basic_ifstream(std::basic_string)'` at `ifStream inFile("C:\\Program Folder\\" + fileName + "\\goalFile.txt");` – ssskh12 Jul 24 '16 at 17:07
  • The only difference with your code is that I inserted a (double) backslash before `"goalFile.txt"`, i.e. `"\\goalFile.txt"`. Why shouldn't that compile, if your original code compiles? – Rudy Velthuis Jul 24 '16 at 18:17
  • @ssskh12: ifstream has no constrctor that takes a std::string, AFAIK, so I wonder how your original program compiled. – Rudy Velthuis Jul 24 '16 at 19:07
  • I changed `string path("C:\\Program Folder\\" + fileName + "\\goalFile.txt");` to `string path("C:\\Program Folder\\" + fileName + "\\goalFile.txt").c_str());` and that fixed everything. I am now able to open the goalFile.txt. Thanks! – ssskh12 Jul 24 '16 at 19:17