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.
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.
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.
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;
}
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.
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
usingnamespace 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"