How to change directory

How to change directory in c++ ( windows )

I want to go to the folder "example2"
and I delete some files
and then return to the original folder


c++ - windows
is bad frined :(

i'm new in c++, and understand how to use code ( i have try it but failed )
Last edited on
Show us your code and we'll try to help.
What problems have you had? Please be more detailed. As a guide, here is a quick example (untested) of how it might look like:
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
#include <windows.h>
#include <iostream>

int main() {
    const int bufferSize = MAX_PATH;
    char oldDir[bufferSize]; // store the current directory
    
    // get the current directory, and store it
    if (!GetCurrentDirectory(bufferSize, oldDir)) {
        std::cerr << "Error getting current directory: #" << GetLastError();
        return 1; // quit if it failed
    }
    std::cout << "Current directory: " << oldDir << '\n';

    // new directory
    const char* newDir = R"(C:\path\to\directory\)"
    if (!SetCurrentDirectory(newDir)) {
        std::cerr << "Error setting current directory: #" << GetLastError();
        return 1; // quit if we couldn't set the current directory
    }
    std::cout << "Set current directory to " << newDir << '\n';

    // Delete some files
    DeleteFile("file1.txt");
    DeleteFile("bin\\file2.exe");
    DeleteFile(R"(data\things\other\file3.zip)");

    // Reset the current directory back to what it was.
    if (!SetCurrentDirectory(oldDir)) {
        std::cerr << "Error resetting current directory: #" << GetLastError();
        return 1;
    }
    std::cout << "Reset current directory. \n";

    // ...

    return 0;
}


DeleteFile: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363915(v=vs.85).aspx
Last edited on
@ NT3: Not bad, you can just use forward slashes for most Windows functions if find that you keep forgetting to double up on the backslashes.
The chdir function works on both POSIX (manpage) and Windows (called _chdir there but an alias chdir exists).

Both implementations return zero on success and -1 on error. As you can see in the manpage, more distinguished errno values are possible in the POSIX variant, but that shouldn't really make a difference.
The directory at the end of the active path is called the current directory; it is the directory in which the active application started, unless it has been explicitly changed. An application can determine which directory is current by calling the GetCurrentDirectory function. It is sometimes necessary to use the GetFullPathName function to ensure the drive letter is included if the application requires it.
Note Although each process can have only one current directory, if the application switches volumes by using the SetCurrentDirectory function, the system remembers the last current path for each volume (drive letter). This behavior will manifest itself only when specifying a drive letter without a fully qualified path when changing the current directory point of reference to a different volume. This applies to either Get or Set operations.
An application can change the current directory by calling the SetCurrentDirectory function.
The following example demonstrates the use of GetCurrentDirectory and SetCurrentDirectory.
Topic archived. No new replies allowed.