Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   ..using system() in C.... (http://www.go4expert.com/forums/using-c-t6457/)

sriraam 20Sep2007 17:53

..using system() in C....
 
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\Sys tem32\Wbem;C:\PROGRA~1\QUICKT~
1\QTSystem\;C:\TC\TC\BIN



PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WS F;.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!


:)

DaWei 20Sep2007 19:00

Re: ..using system() in C....
 
Read this with regard to putting code tags around your code. Then I'll have a look. It's too ugly, now.

shabbir 20Sep2007 20:46

Re: ..using system() in C....
 
Quote:

Originally Posted by DaWei
Read this with regard to putting code tags around your code. Then I'll have a look. It's too ugly, now.

I have done that for you as of now but you should be doing that yourself. Read the content in the link by DaWei

DaWei 21Sep2007 07:58

Re: ..using system() in C....
 
Quote:

strcpy(ptrnew,"PATH=");
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.

sriraam 21Sep2007 11:07

Re: ..using system() in C....
 
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.


All times are GMT +5.5. The time now is 10:01.