[HELP] Change directory windows c++

I simply want my program to go in a directory of my choice and i can't find the answer anywhere to such a simple question.
Why is this not working ?

Can i just write commands in the command prompt simply using a function ?

using windows10/visual studio 2015

when i run this i have the dir of where the program is instead of c:\msi

1
2
3
4
5
6
7
8
9
10
11

int main()
{
	system("cd C:\MSI");
	system("dir");

	char pause;
	cin >> pause;

    return 0;
}
Last edited on
You need to use a double backslash system("cd C:\\MSI");
i tried that too...
On a windows system you might try SetCurrentDirectoryA()
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
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

std::string getDir()
{
    char buff[MAX_PATH];
    int n = GetCurrentDirectoryA(MAX_PATH, buff);
    if (n)
        return buff;
    cout << "get directory failed\n";
    return "";
}

int main()
{
    cout << "Curent dir: " <<  getDir() << '\n';   
    
    if (SetCurrentDirectoryA("C:\\MSI"))
        cout << "Directory change ok\n";
    else
        cout << "Directory change failed\n";

    cout << "Curent dir: " <<  getDir() << '\n';   
       
}

See
https://msdn.microsoft.com/en-us/library/windows/desktop/aa365530%28v=vs.85%29.aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/aa364934%28v=vs.85%29.aspx
Topic archived. No new replies allowed.