LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-26-2003, 08:15 PM   #1
SMB3Master
Member
 
Registered: Aug 2003
Location: USA
Posts: 32

Rep: Reputation: 15
cd in c


How do I change a directory in C? cd doesn't work, it always stays in the directory the program is in.
 
Old 08-26-2003, 08:33 PM   #2
dharmender_rai
Member
 
Registered: Aug 2002
Location: Pune,India
Posts: 39

Rep: Reputation: 15
The shell on which you run a command is itself a process. The directory which it shows would be inherited to all the processes that you invoke from there. But after these invoked child processes change the direcories, the result will not be shown in the parent i.e. shell.
In short the change in directory is process-specific.
 
Old 08-26-2003, 08:44 PM   #3
SMB3Master
Member
 
Registered: Aug 2003
Location: USA
Posts: 32

Original Poster
Rep: Reputation: 15
How can I get a list of files in another directory?
 
Old 08-26-2003, 09:45 PM   #4
devoyage
Member
 
Registered: Aug 2003
Distribution: many
Posts: 37

Rep: Reputation: 15
To change the current working directory of your program (which will be where you are when you run it:
Code:
#include<unistd.h>
chdir("path");
To list the contents of a directory:
Code:
#include<dirent.h>
DIR *dp;
struct dirent *dirp;

if((dp=opendir("path"))==NULL){
   perror();
   exit(1);
}

while((dirp = readdir(dp)) != NULL){
    printf("%s\n",dirp->d_name);
}
also check out the man pages on those functions

Enjoy!
 
Old 08-27-2003, 07:13 AM   #5
SMB3Master
Member
 
Registered: Aug 2003
Location: USA
Posts: 32

Original Poster
Rep: Reputation: 15
Thanks
 
Old 08-27-2003, 01:49 PM   #6
vanquisher
Member
 
Registered: Aug 2003
Location: Hyderabad, India
Posts: 126

Rep: Reputation: 15
chdir("desination directory");

That's because cd is not an executable...
 
Old 08-27-2003, 03:42 PM   #7
SMB3Master
Member
 
Registered: Aug 2003
Location: USA
Posts: 32

Original Poster
Rep: Reputation: 15
I was trying to do system("cd directory").
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration