Well, this morning I tried to write a program to update environment variable's list. And I got stuck at a very crucial point. I think am making some silly mistake. A second opinion will be helpful.
Algorithm:
1) Take complete path of environment variable into a string variable 'env'.
2) Concatenate it with string 'set path=' + env + ';%path%'
3) Pass this string to system().
4) Check if system() is executed correctly.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
char env[200];
printf("Path of env variable: ");
gets(env);
strcpy(env,strcat(strcat("\"set path=",env),"%path%;\"")); // <-- Point of mistake.
puts(env);
if ( system(env) != -1){ // <-- Point of mistake.
puts("Environment Variable set.");
system("PATH");
}else{
puts("Failed to set environment variable.");
}
getch();
return 0;
}
Thanks :-)