system("CD"). Help please :(

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2009
Posts: 12
Reputation: licktress is an unknown quantity at this point 
Solved Threads: 0
licktress licktress is offline Offline
Newbie Poster

system("CD"). Help please :(

 
0
  #1
Aug 6th, 2009
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.

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

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

Any help would be greatly appreciated =D
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: system("CD"). Help please :(

 
0
  #2
Aug 6th, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 106
Reputation: jen140 is an unknown quantity at this point 
Solved Threads: 5
jen140 jen140 is offline Offline
Junior Poster

Re: system("CD"). Help please :(

 
0
  #3
Aug 6th, 2009
You could use something like :
cd PATH & dir & pause
In the system function .
Last edited by jen140; Aug 6th, 2009 at 10:05 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: system("CD"). Help please :(

 
0
  #4
Aug 6th, 2009
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.)
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: system("CD"). Help please :(

 
0
  #5
Aug 6th, 2009
There is quite a bit of overhead, but you can use some of the VUL functions of VXL.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: system("CD"). Help please :(

 
0
  #6
Aug 6th, 2009
Originally Posted by daviddoria View Post
There is quite a bit of overhead, but you can use some of the VUL functions of VXL.
?? why ?? All he needs is chdir() function.
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 12
Reputation: licktress is an unknown quantity at this point 
Solved Threads: 0
licktress licktress is offline Offline
Newbie Poster

Re: system("CD"). Help please :(

 
0
  #7
Aug 6th, 2009
Originally Posted by Duoas View Post
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

  1. string cFolder;
  2. cout << "Enter a folder path to browse:" << endl;
  3. cin >> cFolder;
  4. chdir(cFolder);
  5. opendir(cFolder);
  6. system("PAUSE");

Oh, and yes I have #included all the neccessary files.
Is there any way to chdir a variable?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: system("CD"). Help please :(

 
0
  #8
Aug 6th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 949
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: system("CD"). Help please :(

 
0
  #9
Aug 6th, 2009
Because they require a const char* not a std::string. Try string.c_str()

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
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 12
Reputation: licktress is an unknown quantity at this point 
Solved Threads: 0
licktress licktress is offline Offline
Newbie Poster

Re: system("CD"). Help please :(

 
0
  #10
Aug 7th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC