| | |
system("CD"). Help please :(
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2009
Posts: 12
Reputation:
Solved Threads: 0
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.
Something like that. Does anyone know a way to do this? I keep getting errors.
Any help would be greatly appreciated =D
C++ Syntax (Toggle Plain Text)
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!
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!
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 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.)
Last edited by Ancient Dragon; Aug 6th, 2009 at 10:11 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Aug 2009
Posts: 12
Reputation:
Solved Threads: 0
•
•
•
•
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!

C++ Syntax (Toggle Plain Text)
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. Last edited by Ancient Dragon; Aug 6th, 2009 at 11:37 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Because they require a const char* not a std::string. Try string.c_str()
And remove that system() call at the end.
And remove that system() call at the end.
Last edited by MosaicFuneral; Aug 6th, 2009 at 11:38 pm. Reason: Typo.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
![]() |
Similar Threads
- System 32 at startup and "dwn"/"down" pop up at startup (Viruses, Spyware and other Nasties)
- System 32 at startup and "dwn"/"down" pop up at startup (Viruses, Spyware and other Nasties)
- "Invalid" system disk...." (Storage)
- System 32 at startup and "dwn"/"down" pop up at startup (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: Define abstart overridable method
- Next Thread: exercise on recursion...
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






