I'm very new at C++ and would like to know how I would change a directory via user input? This will be a Win32 Project and not a Win32 application.

I know I can use (as standard - and without user input) the chdir("\\dir"); - but how do I use this with user input?

Thanks in advance! :)

Recommended Answers

All 4 Replies

usethe string header so you can get the path from the user then pass it to chdir like this

chdir(path.c_str());

Chris

Thanks for your reply!!

Sorry if I am being thick, but is this correct (some of the script has been removed)?

#include <string.h>

using namespace std;

string entry;

cout << "1. Enter FULL PATH to directory: ";
cin >> entry;

chdir(path.entry_str());

cin.get();

	return 0;
}

Thanks again for your help!

This should help you out

http://msdn.microsoft.com/en-us/library/bf7fwze1(VS.80).aspx
I would read that but this is how to use it anyway

#include <string>
#include <direct.h>

using namespace std;

int main(void){
    
    string hello;
    
    hello = "C:\\";
    
    _chdir(hello.c_str());
    
    cin.get();
    return 0;
    }

Chris

Hi,

Thank you very much for that. It now works!!!!

Using:

string user;
string entry;

cout << "2. Enter FULL PATH to DIR: ";
cin >> entry;

user = entry;

_chdir(user.c_str());

Thanks for your help!!!!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.