Create folder with fstream

I was wanting to learn more about fstream and how to manipulate files better. I was wondering how would i simply create a folder with C++? i did it before but i dont have the code anymore.

EDIT: Also i want to be able to save the file with a uniqe name that the user gives it. Maybe the user enters a string and then its saves as that.
Last edited on
1
2
3
4
5
#include <sys/types.h>
#include <sys/stat.h>

getline(cin, stringName);
mkdir(stringName.c_str(), aMacroThatICantRemember);
mkdir was not declared in this scope and a macro i can remember you mean its a number right? i have 200
Last edited on
no i mean a macro but i cant remeber the data type. and did u remember using namespace std; and did u remember to include... oh wait ur overloading. so forget everything this post said so far. so i dont mean a macro i mean a data type my mistake its mode_t so do above mkdir mode_t variableName; and then mkdir(strName.c_str(), variableName);
soooooo something like this?:

and yes i did include thos 2 headers but still get an error.

So how would it look in this 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 <sys/stat.h>


using namespace std;

int main()
{
    int XP = 34;
    int money = 400;
    string name;
    string str;

    ifstream in;
    in.open("textfile.txt");

    in >> XP;
    in >> money;
    in >> name;

    if(in.fail())
    {
        cout << "Could not load from text file" << endl;
    }

    cout << "Enter Name" << endl;
    getline(cin, name);
    cout << "\n";

    getline(cin, str);
    mkdir(str.c_str(), str);

    ofstream out;
    out.open("textfile.txt");

    out << XP << endl;
    out << money << endl;
    out << name << endl;

    out.close();
}
ah but u didnt include the two headers i only see sys/stat.h and no sys/types.h
and i said create a mode_t variable and u didnt do that.
I did have the other one but it still didnt matter and i read somewhere that it weasnt needed.

Like this?

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
45
#include <iostream>
#include <fstream>
#include <string>
#include <sys/stat.h>


using namespace std;

int main()
{
    int XP = 34;
    int money = 400;
    string name;
    string str;
    mode_t mt;

    ifstream in;
    in.open("textfile.txt");

    in >> XP;
    in >> money;
    in >> name;

    if(in.fail())
    {
        cout << "Could not load from text file" << endl;
    }

    cout << "Enter Name" << endl;
    getline(cin, name);
    cout << "\n";

    getline(cin, str);
    mkdir(str.c_str(), str);

    ofstream out;
    out.open("textfile.txt");

    out << XP << endl;
    out << money << endl;
    out << name << endl;

    out.close();
}
yes it does matter and if u do have it then why do i not see it in the source? and on line 34 it is mkdir(str.c_str(), mt);
Ok i made the changes and the compiler says

C:\Users\Chay Hawk\Desktop\fstream practice\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\fstream practice\main.cpp|35|error: 'mkdir' was not declared in this scope|
||=== Build finished: 1 errors, 0 warnings ===|
show ur complete source
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
45
46
#include <iostream>
#include <fstream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>


using namespace std;

int main()
{
    int XP = 34;
    int money = 400;
    string name;
    string str;
    mode_t mt;

    ifstream in;
    in.open("textfile.txt");

    in >> XP;
    in >> money;
    in >> name;

    if(in.fail())
    {
        cout << "Could not load from text file" << endl;
    }

    cout << "Enter Name" << endl;
    getline(cin, name);
    cout << "\n";

    getline(cin, str);
    mkdir(str.c_str(), mt);

    ofstream out;
    out.open("textfile.txt");

    out << XP << endl;
    out << money << endl;
    out << name << endl;

    out.close();
}
dont know sorry
Try changing
mkdir(str.c_str(), mt);
to
_mkdir(str.c_str());
Topic archived. No new replies allowed.