..using system() in C....

Discussion in 'C' started by sriraam, Sep 20, 2007.

  1. sriraam

    sriraam New Member

    Joined:
    Sep 19, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    india
    i wanted to use system() defined in process.h to fiddle with folders and stuff using my C program.
    but system() never succeeds ..it returns -1.
    this is my code and my output.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<dos.h>
    #include<string.h>
    void main(int argc,char *argv[],char *env[])
    {
    int i=0;
    char *ptr,*ptrnew;
    clrscr();
    /*ptr=getenv("PATH");
    strcpy(ptrnew,"PATH=");
    strcat(ptrnew,ptr);
    strcat(ptrnew,";C:\\TC\\TC\\BIN");
    putenv(ptrnew);
     */
    printf("%d\n\n",system("dir"));
    for(;env[i]!=NULL;i++)
    printf("%s\n",env[i]);
    }
    
    OUTPUT:

    -1

    COMSPEC=C:\WINDOWS\SYSTEM32\COMMAND.COM
    ALLUSERSPROFILE=C:\DOCUME~1\ALLUSE~1
    APPDATA=C:\DOCUME~1\HARIHA~1\APPLIC~1
    CLASSPATH=.;C:\Program Files\QuickTime\QTSystem\QTJava.zip
    CLIENTNAME=Console
    COMMONPROGRAMFILES=C:\PROGRA~1\COMMON~1
    COMPUTERNAME=SRIRAAMX-3F75EB
    FP_NO_HOST_CHECK=NO
    HOMEDRIVE=C:
    HOMEPATH=\Documents and Settings\hariharan
    LOGONSERVER=\\SRIRAAMX-3F75EB
    NUMBER_OF_PROCESSORS=1
    OS=Windows_NT


    PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\PROGRA~1\QUICKT~
    1\QTSystem\;C:\TC\TC\BIN



    PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
    PROCESSOR_ARCHITECTURE=x86
    PROCESSOR_IDENTIFIER=x86 Family 15 Model 3 Stepping 3, GenuineIntel
    PROCESSOR_LEVEL=15
    PROCESSOR_REVISION=0303
    PROGRAMFILES=C:\PROGRA~1
    PROMPT=$P$G
    QTJAVA=C:\Program Files\QuickTime\QTSystem\QTJava.zip
    SESSIONNAME=Console
    SYSTEMDRIVE=C:
    SYSTEMROOT=C:\WINDOWS
    TEMP=C:\WINDOWS\TEMP
    TMP=C:\WINDOWS\TEMP
    USERDOMAIN=SRIRAAMX-3F75EB
    USERNAME=hariharan
    USERPROFILE=C:\DOCUME~1\HARIHA~1
    __COMPAT_LAYER=EnableNXShowUI
    BLASTER=A220 I5 D1 P330 T3

    ..................................................................................................................................
    the comment part was also used to include the environment variable at runtime.
    but no fruits even then. :confused:
    the directory of my tc.exe is C\TC\TC\BIN
    as u can see its included in the environment pointer list.
    pls help me out.
    peace!


    :)
     
    Last edited by a moderator: Sep 20, 2007
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Read this with regard to putting code tags around your code. Then I'll have a look. It's too ugly, now.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I have done that for you as of now but you should be doing that yourself. Read the content in the link by DaWei
     
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    You do not understand pointers. You have declared a pointer, but a pointer must point at a valid storage area. To be useful, it is part of a pair: the pointer, and where the pointer points. You have not provided the latter part. Read this and think about it. Read it two or three times, if necessary.
     
  5. sriraam

    sriraam New Member

    Joined:
    Sep 19, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    india
    u dont seem to understand my problem sir.pls read my question properly.
    i know a lot more about pointers than u seem to think.
    my compiler seems to allocate memory .(its is not evident from the output)
    but its on comment anyway.
    but as u can see ,path value in output lists c:/tc/tc/bin
    i added it from mycomputer.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice