Changing directories from a C console program

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • oddstray

    #1

    Changing directories from a C console program

    Hi,

    I'm using WinXP. I wrote a short C utility in which I want to change
    directories and run programs. When I use the system() method and pass
    in the same command that works in a cmd.exe session, it claims "Unable
    to locate the directory". This happens for directories with and
    without spaces in their names. Can anyone tell me what's wrong?
    Thanks.


    B


    Here's the code snippet that fails:

    if( _chdir( "C:" ) != 0 )
    printf( "Unable to locate the directory: %s\n", "C:" );
    else
    system( "dir *.*");

    // WORKS THIS FAR

    if( _wchdir( "cd Perl" ) != 0 )
    printf( "Unable to locate the directory: %s\n", "Perl" );
    else
    system( "dir *.*");

    if( _wchdir( "cd C:\\Perl" ) != 0 )
    printf( "Unable to locate the directory: %s\n", "C:\\Perl" );
    else
    system( "dir *.*");

    // 'UNABLE TO LOCATE' EITHER OF THESE ALTHOUGH THIS DIRECTORY DOES
    EXIST
  • Jack Klein

    #2
    Re: Changing directories from a C console program

    On 26 Aug 2004 17:57:50 -0700, oddstray@gmail. com (oddstray) wrote in
    comp.lang.c:
    [color=blue]
    > Hi,
    >
    > I'm using WinXP. I wrote a short C utility in which I want to change
    > directories and run programs. When I use the system() method and pass
    > in the same command that works in a cmd.exe session, it claims "Unable
    > to locate the directory". This happens for directories with and
    > without spaces in their names. Can anyone tell me what's wrong?
    > Thanks.[/color]

    Not here, we can't. The standard C language has no support for
    directories at all. Not all systems have them and they are
    implemented in vastly different ways on some platforms that do.
    [color=blue]
    > Here's the code snippet that fails:
    >
    > if( _chdir( "C:" ) != 0 )
    > printf( "Unable to locate the directory: %s\n", "C:" );
    > else
    > system( "dir *.*");
    >
    > // WORKS THIS FAR
    >
    > if( _wchdir( "cd Perl" ) != 0 )
    > printf( "Unable to locate the directory: %s\n", "Perl" );
    > else
    > system( "dir *.*");
    >
    > if( _wchdir( "cd C:\\Perl" ) != 0 )
    > printf( "Unable to locate the directory: %s\n", "C:\\Perl" );
    > else
    > system( "dir *.*");
    >
    > // 'UNABLE TO LOCATE' EITHER OF THESE ALTHOUGH THIS DIRECTORY DOES
    > EXIST[/color]

    There are no functions named "_chdir" or "_wchdir" in the standard C
    library, they are extensions provided by your compiler. You need to
    ask about this in one of Microsoft's support groups in the
    news:microsoft. public.vc.* family, or a group like
    news:comp.os.ms-windows.program mer.win32.

    It is a Windows issue, not a C language one.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c++-faq-lite/
    alt.comp.lang.l earn.c-c++

    Comment

    • Keith Thompson

      #3
      Re: Changing directories from a C console program

      oddstray@gmail. com (oddstray) writes:[color=blue]
      > I'm using WinXP. I wrote a short C utility in which I want to change
      > directories and run programs. When I use the system() method and pass
      > in the same command that works in a cmd.exe session, it claims "Unable
      > to locate the directory". This happens for directories with and
      > without spaces in their names. Can anyone tell me what's wrong?
      > Thanks.
      >
      >
      > B
      >
      >
      > Here's the code snippet that fails:
      >
      > if( _chdir( "C:" ) != 0 )
      > printf( "Unable to locate the directory: %s\n", "C:" );
      > else
      > system( "dir *.*");
      >
      > // WORKS THIS FAR
      >
      > if( _wchdir( "cd Perl" ) != 0 )
      > printf( "Unable to locate the directory: %s\n", "Perl" );
      > else
      > system( "dir *.*");
      >
      > if( _wchdir( "cd C:\\Perl" ) != 0 )
      > printf( "Unable to locate the directory: %s\n", "C:\\Perl" );
      > else
      > system( "dir *.*");
      >
      > // 'UNABLE TO LOCATE' EITHER OF THESE ALTHOUGH THIS DIRECTORY DOES
      > EXIST[/color]

      We don't know. The _chdir() and _wchdir() functions aren't defined in
      standard C; neither are directories, for that matter. You might try a
      Windows-specific newsgroup. (But first, you might ask yourself
      whether _wchdir() expects a directory name or a command string.)

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
      We must do something. This is something. Therefore, we must do this.

      Comment

      • jacob navia

        #4
        Re: Changing directories from a C console program

        oddstray wrote:[color=blue]
        > Hi,
        >
        > I'm using WinXP. I wrote a short C utility in which I want to change
        > directories and run programs. When I use the system() method and pass
        > in the same command that works in a cmd.exe session, it claims "Unable
        > to locate the directory". This happens for directories with and
        > without spaces in their names. Can anyone tell me what's wrong?
        > Thanks.
        >
        >
        > B
        >
        >
        > Here's the code snippet that fails:
        >
        > if( _chdir( "C:" ) != 0 )
        > printf( "Unable to locate the directory: %s\n", "C:" );
        > else
        > system( "dir *.*");
        >
        > // WORKS THIS FAR
        >
        > if( _wchdir( "cd Perl" ) != 0 )
        > printf( "Unable to locate the directory: %s\n", "Perl" );
        > else
        > system( "dir *.*");
        >
        > if( _wchdir( "cd C:\\Perl" ) != 0 )
        > printf( "Unable to locate the directory: %s\n", "C:\\Perl" );
        > else
        > system( "dir *.*");
        >
        > // 'UNABLE TO LOCATE' EITHER OF THESE ALTHOUGH THIS DIRECTORY DOES
        > EXIST[/color]

        wchdir expects a wide character set string, not a multi byte one.
        Hence, it doesn't find any directory you pass it.

        Rule:
        Use the documentation when something doesn't work.

        Comment

        • Alan Balmer

          #5
          Re: Changing directories from a C console program

          On 26 Aug 2004 17:57:50 -0700, oddstray@gmail. com (oddstray) wrote:
          [color=blue]
          >
          >I'm using WinXP. I wrote a short C utility in which I want to change
          >directories and run programs. When I use the system() method and pass
          >in the same command that works in a cmd.exe session, it claims "Unable
          >to locate the directory". This happens for directories with and
          >without spaces in their names. Can anyone tell me what's wrong?
          >Thanks.[/color]

          Off topic here. Try a Windows programming group. However, I note that
          you have one method which works - why not use it?

          {OT} I wouldn't expect the system calls to work. You spawn a shell,
          change directory in that shell, then close the shell and return to the
          program you spawned it from. {/OT]

          --
          Al Balmer
          Balmer Consulting
          removebalmercon sultingthis@att .net

          Comment

          • Kenneth Brody

            #6
            Re: Changing directories from a C console program

            oddstray wrote:
            [...][color=blue]
            > if( _wchdir( "cd Perl" ) != 0 )[/color]
            [...]

            Do you really have a directory called "cd Perl"?

            If so, then something else is wrong, and you'll need to ask help in a
            Windows-related group.

            If not, then I hope you see the problem now.

            --
            +-------------------------+--------------------+-----------------------------+
            | Kenneth J. Brody | www.hvcomputer.com | |
            | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
            +-------------------------+--------------------+-----------------------------+


            Comment

            Working...