Hey guys. I am trying to make a program that prompts the user to enter in a directory path, and then the program goes to that directory and displays what is inside.

string cString;
cin >> cString;
system("cd " cString); //this is what i am having trouble with.
system("DIR");
system("PAUSE");

Something like that. Does anyone know a way to do this? I keep getting errors. :(

Any help would be greatly appreciated =D

The first thing you need to do is break your dependence on the system() function. It spawns a new shell (cmd.exe, probably), changes the directory for that new shell, then terminates. Your program's current working directory remains the same.

Most C/C++ compilers provide versions of the <unistd.h> function chdir().

You can get information about the files in the directory using opendir(), etc. in <dirent.h>, and stat() in <sys/stat.h>. For all these, you'll also have to #include <sys/types.h>.

If you want to do it the Microsoft Windows way, you'll have to #include <windows.h> and use SetCurrentDirectory() and the FindFirstFile()/FindNextFile() functions.

Good luck!

You could use something like :
cd PATH & dir & pause
In the system function .

The system function will not work for what you want to do because as soon as system() returns to your program the operating system discards the results of cd and resets the current directory to what it was before the program called system(). This is the same identical behavior of *nix operating systems and *nix shells.

The only way to make your program work is to call the standard C function chdir(), as previously recommended (assuming your operating system supports it, and some do not.)

The first thing you need to do is break your dependence on the system() function. It spawns a new shell (cmd.exe, probably), changes the directory for that new shell, then terminates. Your program's current working directory remains the same.

Most C/C++ compilers provide versions of the <unistd.h> function chdir().

You can get information about the files in the directory using opendir(), etc. in <dirent.h>, and stat() in <sys/stat.h>. For all these, you'll also have to #include <sys/types.h>.

If you want to do it the Microsoft Windows way, you'll have to #include <windows.h> and use SetCurrentDirectory() and the FindFirstFile()/FindNextFile() functions.

Good luck!

I've tryed this, but the chdir and opendir functions are not accepting string variables. This is my current code, if it will help you help me :)

string cFolder;
cout << "Enter a folder path to browse:" << endl;
cin >> cFolder;
chdir(cFolder);
opendir(cFolder);
system("PAUSE");

Oh, and yes I have #included all the neccessary files.
Is there any way to chdir a variable?

So?? All you have to do is pass the char* of string. chdir(cFolder.c_str(); . And note that the >> operator will not allow spaces in whatever it is that you type. call getline() if you need the spaces.

Okay, it works now, however, if the string has any spaces in it, it doesn't work.

Does anyone know how to add spaces to a string variable?

Thanks for all the help :)

The function i talked about is only used to work with strings that you already have, so if you need to mannualy add the space or other content to the given string you can use that function
If i'm not mistaken the user can introduce the string with space without any need to use other functions.

You don't need to use strcat()... Please don't suggest using the C string library to manipulate C++ strings.

AD already addressed your question. You should be using getline() to get user input:

string dirpath;
getline( cin, dirpath );  // gets everything, including spaces

Hope this helps.

Okay, it works now, however, if the string has any spaces in it, it doesn't work.

Does anyone know how to add spaces to a string variable?

Thanks for all the help :)

You could have saved yourself a lot of time had you bothered to read the post I made over 10 hours ago :icon_eek:

This article has been dead for over six months. Start a new discussion instead.