Using fstream to open a directory and grab file names. Possible?

I am making a virtual machine and i need to open a directory (progs/Input) that holds all the assembler .s files. Once the directory is open i need to get the names of all the files in the directory so i can open them. Once the .s file is open i modify the name by erasing the 's' and adding a 'o'. The problem i am having is that it seems i cannot use fstream to open a directory and then grab the names of files inside, the file seems to not be opening and my exit(20) triggers. I know i can use

 
system("ls *.s > progs/Input");


and that will give me all the file names inside the directory but i want to know if there is a way to get my code working (the code above is some code my teacher gave the class) or if my way will never work. The code that follows are the relevant bits of my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
        string file_list, file_list_o;

        fstream progs, inputfile, outputfile;
        progs.open("progs/Input/", ios::out);
        if (!progs.is_open()) { cout << "Directory progs/Input failed to open!" << endl; exit(20); }

        progs >> file_list;
        while (!progs.eof()) {
            file_list_o = file_list.substr(0, file_list.length() - 1);
            file_list_o += "o";

            file_list_o = "progs/Object/" + file_list_o;
            outputfile.open((file_list_o).c_str(), ios::out);
            if (!outputfile.is_open()) { 
                cout << "A .o file failed to open. /n" << "File Name: " << file_list << endl;
                exit(21); 
             }

            file_list = "progs/Input/" + file_list;
            inputfile.open((file_list).c_str(), ios::in);
            if (!inputfile.is_open()) {
                cout << "An Assembler file in progs failed to open. /n File name: " << file_list << endl; 
                exit(22); 
            }

            inputfile.close();
            outputfile.close();

            progs >> file_list;
        }

        progs.close();
}



Thanks
Last edited on
Alas, no, directory entry files are special and there is no convenient interface for them like you describe in C++.

For now, use Boost Filesystem. C++17 will include the same facility.

If this is just a school homework and your professor wants this behavior, ask him how he wants you to obtain the information.

You may have to add code using OS facilities. Both Windows and nixen are very easy to use for this.

Hope this helps.
It is not possible to use fstream for directories. See:

http://www.cplusplus.com/reference/fstream/fstream/?kw=fstream

Either you use what the teacher provides (Input would be a file not a directory) or you system dependant functions or external libraries such as boost.
Well i gotta change my code a bit then. Shouldn't be too hard. Thanks for the quick responses!
Topic archived. No new replies allowed.