943,398 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 6049
  • C RSS
Aug 23rd, 2003
0

i = system ("cd c:\text"); :(

Expand Post »
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main ()
{
  int i,j;
  puts ("Trying to execute command CD ");
  i = system ("cd c:\text"); 
  if (i==-1) puts ("Error executing CD");
  else puts ("Command successfully executed");

 puts ("Trying to execute command del ");
  j = system ("del *.txt"); 
  if (j==-1) puts ("Error executing del");
  else puts ("Command successfully executed");

 getchar();
  return 0;
}

It won't work. How do I make this go to a specific directory? I know if i just make it i = system ("cd"); that it will print the current directory. Is there a way to change directories this way?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kyle is offline Offline
3 posts
since Aug 2003
Aug 23rd, 2003
0
Re: i = system ("cd c:\text"); :(
system actually starts a new process, does what you want, then closes that process. in fact, if you really think about it, what does it mean to "change directory"; "change directory" or cd is a Shell implemented feature on the *system* level. With this in mind, you're best bet is to use the standard unix library.

#include <unistd.h>

and the function chdir(char *path)

man chdir
man getcwd


system is the worst function of all time, never use it.
Last edited by subtronic; Aug 23rd, 2003 at 2:56 am.
Reputation Points: 44
Solved Threads: 1
Junior Poster
subtronic is offline Offline
117 posts
since Aug 2003
Aug 24th, 2003
0

Re: i = system ("cd c:\text"); :(

Where can I get unistd.h?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kyle is offline Offline
3 posts
since Aug 2003
Aug 24th, 2003
0

Re: Re: i = system (

Quote originally posted by kyle ...
Where can I get unistd.h?
I'm sorry, at a closer look you are using Windows...I thought you were actually using C on a Unix System. I'm not sure how you'd do it with the Win32 API, I'd imagine it is quite similar.
Last edited by subtronic; Aug 24th, 2003 at 11:45 pm.
Reputation Points: 44
Solved Threads: 1
Junior Poster
subtronic is offline Offline
117 posts
since Aug 2003
Aug 25th, 2003
0

Re: Re: i = system (

Quote originally posted by waterdog ...
Shouldn’t system(“cd c:\text”);
be system(“cd c:\\text”);
Yes.

chdir("c:\\text");


By the way subtronic, on windows the chdir() function is in dir.h. Thanks alot for all the help guys
Last edited by kyle; Aug 25th, 2003 at 11:49 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kyle is offline Offline
3 posts
since Aug 2003

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: Help with gui loop.
Next Thread in C Forum Timeline: Creating a locking program





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


Follow us on Twitter


© 2011 DaniWeb® LLC