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

sriraam's Avatar, Join Date: Sep 2007
Newbie Member
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.
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 shabbir; 20Sep2007 at 20:45.. Reason: Code block
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Read this with regard to putting code tags around your code. Then I'll have a look. It's too ugly, now.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
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.
0
sriraam's Avatar, Join Date: Sep 2007
Newbie Member
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.