943,724 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1023
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 6th, 2009
0

system("CD"). Help please :(

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
licktress is offline Offline
12 posts
since Aug 2009
Aug 6th, 2009
0

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

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!
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Aug 6th, 2009
0

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

You could use something like :
cd PATH & dir & pause
In the system function .
Last edited by jen140; Aug 6th, 2009 at 10:05 pm.
Reputation Points: 11
Solved Threads: 6
Junior Poster
jen140 is offline Offline
116 posts
since Jan 2009
Aug 6th, 2009
0

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

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Aug 6th, 2009
0

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

There is quite a bit of overhead, but you can use some of the VUL functions of VXL.
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Aug 6th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by daviddoria ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Aug 6th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by Duoas ...
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

C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
licktress is offline Offline
12 posts
since Aug 2009
Aug 6th, 2009
0

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

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Aug 6th, 2009
0

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

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.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Aug 7th, 2009
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
licktress is offline Offline
12 posts
since Aug 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Define abstart overridable method
Next Thread in C++ Forum Timeline: exercise on recursion...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC