create folder and put your files inside the folder using c++

Hi,

I'm a newbie. I wanted to know how to create a folder using c++. After creating a folder I would access a function which will give me a set of *.txt files which needs to be outputted to the newly created folder.

Examples are most welcomed.

Thanks in Advance.
Last edited on
This is all about creating a folder. It's Ok.

But I need to put all the *.txt files which are printed into the newly created folder.

The post you have posted is saying about creating a folder. But It doesn't say anything about the location of the printing .txt files will be directed to the specific folder which is created.

So, I think this is not what I need.
Please have a look at a function shown below.

Here, It does print the files (Example: Time step-1, Time step-2,.........Time step n) . I want to create a folder named ftcs (for example) first and then I need to print the files inside the folder what I created.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
              int ftcs (float aa,float bb,float a,float d,float* u,int m,float o)
                 {
                         for (int k=1;k<=aa/bb+1;k++)
                              {
                                   ofstream myfile;
                                   stringstream ss;
                            	   ss << "Time Step-" << k*bb << ".txt";
                            	   myfile.open (ss.str().c_str());
                                   cout<<"\nTime Step:"<<k*bb<<" Sec\n";   
                                   std::copy (u,u+m,un);       
                                   for (int j=1;j<a/d;j++)
                                     {
                                            u[j]=un[j]+o*(un[j+1]-(2*un[j])+un[j-1]);
                              			    myfile<<std::setprecision(16)<<u[j]<<"\t";
                                            myfile<<j*d<<endl;           		  
                                            //cout<<"\n"<<j<<"\t"<<u[j];
                                     }myfile.close(); //cout<<"\n";
                               }     
                 }


I understand clearly that I can create folder by using similar kind of code shown below. But i want a clear idea how would I proceed with creating the files in the specific folder I create.

1
2
3
4
5
6
    #include <direct.h>
    int main()
    {
          mkdir("c:/myfolder");
          return 0;
    }
Last edited on
Line 8 of your code passes a string to ofstream::open(). Depending on your ofstream implementation it might be just happy with "c:/myfolder/foo.txt".
How can I create a folder in an c++ program executable location?

but as per my last post. It creates in "c:/myfolder".

If you have executable in "d:/cpp" and if i need to create a folder in location "d:/cpp" without defining the location.

Is it possible. If so, How?
Last edited on
There are two different directories to consider. One is the directory where the executable is located. The other is the current working directory (cwd).
Here's a short program which should display both. Depending upon your system, these may or may not be the same.

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
#include <iostream>
#include <string>
#include <dir.h>

using namespace  std;

std::string getcwd();
std::string getexedir(const char * s);

int main(int argc, char *argv[])
{
    cout << getexedir(argv[0]) << endl;
    cout << getcwd() << endl;

    return 0;
}

std::string getcwd()
{
    char result[1024];
    return std::string(getcwd(result, sizeof(result)));
}

std::string getexedir(const char * s)
{
    string exe(s);
    size_t pos = exe.rfind('\\');
    if (pos != string::npos)
        exe = exe.substr(0,pos);
    return exe;
}
Hi Chervil,

The program gives the location of either executable or cwd.

But how to add the new folder to cwd or executable.

can you explain me that.
I fixed my issue with the solution below.

But now I would need to place my files inside the directory which i created.

How would I change the directory where my files are to be printed out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <sstream>
#include <string>
#include <direct.h> // for _mkdir
using namespace std;

int main()
{
    int counter = 0;
    while(counter < 10)
    {
        ostringstream oss;
        oss << "ABCDEF " << counter;
        _mkdir(oss.str().c_str());
        counter++;
    }
    return 0; // main returns 0 even if it's not explicit,
              // so you might as well make this clear!
}
Filename can contain a path component. Path can be either relative or absolute. The system actually concatenates cwd (an absolute path) with a filename that has either relative or no path in order to create an absolute path.

If a system library provides getcwd, it probably has setcwd as well.

[code]// when getcwd returns "C:/foo"
myfile.open("C:/foo/bar.txt");
myfile.open("bar.txt"); // same as "C:/foo/bar.txt"
myfile.open("gaz/bar.txt"); // same as "C:/foo/gaz/bar.txt"
myfile.open("../Users/bar.txt"); // same as "C:/Users/bar.txt"
Topic archived. No new replies allowed.