chdir (mystring); changing DIR but not staying there.

This is a discussion on chdir (mystring); changing DIR but not staying there. within the C Programming forums, part of the General Programming Boards category; Hello, In my code if I use chdir (".."); then issue system ("dir"); it shows a list of the previous ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    12

    chdir (mystring); changing DIR but not staying there.

    Hello,

    In my code if I use chdir (".."); then issue system ("dir"); it shows a list of the previous directory which is what I want.

    However if I pass chdir my built string chdir (message) it will go to that and then go back to the original dir. How can I make it remain in that directory?

    P.S. I know message is working because if I concatenate DIR to it and pass it to system (message) then it will list the contents of the directory from message.

    Also directorylist.txt has path names in it C:\Program Files\hi after message is created it, messages value is C:\"Program files"\"hi"

    Here is the code.

    Code:
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <dos.h>
    #include <string.h>
    int changedir();
    
    int main(){
    	
    	changedir();
    	
    	return 0;
    }
    
    int changedir(){
      char buffer[200] = "";  /* declare a char array */
      char message[200] = "";
      char field[200] = "";
      char result[200] = "";
      int n, first_run = 1;
      FILE *file;  /* declare a FILE pointer  */
      file = fopen("directorylist.txt", "r"); 
      /* open a text file for reading */
      
      if(file==NULL) {
        printf("Error: can't open file.\n");
        /* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */
        return 1;
      }
      else {
    	fgets(buffer, 200, file);
    	const char *ptr = buffer;
    	while ( sscanf(ptr, "%31[^\\]%n", field, &n) == 1 ){ //delimiters
    		ptr += n; /* advance the pointer by the number of characters read */
            if ( *ptr != '\\' ){
                break; /* didn't find an expected delimiter, done? */
            }else{    
    			if (first_run) //so message isn't C:"\" but instead C:\"
    				;
    			else
    				strcat(field, "\"");
    			
    			strcat(message, field);
    		    strcat(message, "\\\"");
    		}
    		 ++ptr; /* skip the delimiter */
    		 first_run = 0;
    	}
    	//sprintf (result, "cd %s", message); // append CD to message
    	//printf ("%s", message);
    	//system (result);
    	chdir (message);
    	//system ("chdir c:\\");
    	system ("dir");
    	
        fclose(file);
        return 0;
      }
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,531
    > chdir (message);
    It seems to me like message is full of \\ and "
    It doesn't need them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    12
    well if i do a dir with message it lists all the files of the dir in message.

    even if i type system ("cd c:\fun"); it won't stay there. not even chdir ("c:\fun"); will stay there, it just enters and leaves.

    the only time it remains in the directory is when I use chdir ("..");

    so how to stay in that directory using my message which is equivalent to chdir ("C:\fun");


    so basically the code works but i want it to stay in the directory.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Depending on the OS, you can't. For security reasons (mostly), the OS gives your process a copy of the environment variables to play with. You can't change the shell's environment from within your program.
    If you understand what you're doing, you're not learning anything.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,531
    chdir(), if you're doing it right will change the directory and stay there until you chdir() again.

    I would suggest that chdir() returns a status value, which you should check. My guess is that the string you're passing is junk, so it appears to do nothing (when in fact it is complaining).

    > even if i type system ("cd c:\fun"); it won't stay there
    That's because it doesn't change the directory of the current process. It runs another process, which changes it's own directory, then promptly exits, leaving you where you are.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    12
    so without all my code. how can i change to a specified directory and stay their. then do a dir to see the contents of that directory?

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    When you say "stay there" do you mean after your program exits or while you're still in the program?
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    12
    while i'm in the program.

    so if i change the directory to C:\fun
    i can then list the directory and write something there.
    then go to C:\happy
    then move a text file in there etc...

    i want the directory changes to last for the life of my program.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    They do. As Salem suggested check the return value of chdir().
    If you understand what you're doing, you're not learning anything.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,531
    chdir() works spiffy in this FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    12
    your right.
    great thanks. That code is better than mine.
    Just out of curiousity how can I edit the code below so chdir (buffer); will work?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <dos.h>
    #include <string.h>
    int changedir();
    
    int main(){
    	
    	changedir();
    	
    	return 0;
    }
    
    int changedir(){
      char buffer[200] = "";  /* declare a char array */
     
      FILE *file;  /* declare a FILE pointer  */
      file = fopen("directorylist.txt", "r"); 
      /* open a text file for reading */
      
      if(file==NULL) {
        printf("Error: can't open file.\n");
        /* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */
        return 1;
      }
      else {
    	fgets(buffer, 200, file);
    	
    	chdir (buffer);
    	system ("dir");
        
    	fclose(file);
        return 0;
      }
    }

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,531
    You need to strip off the \n that fgets() returns in the buffer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    What compiler are you using? On all compilers that I can find, chdir() is in <unistd.h> (for POSIX) or <direct.h> (for Borland and Watcom, etc), never in <dos.h>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Changing the active dir
    By pinkcheese in forum Windows Programming
    Replies: 5
    Last Post: 05-24-2002, 01:55 AM
  5. changing "active" dir
    By Shadow in forum C Programming
    Replies: 4
    Last Post: 04-23-2002, 10:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21