Hi everyone. I got a total newbe question and i am new to c programming. I want to write a program that will executing commands , written in separate text file.
Lets say, in perl i got something like that:

open (F,"<file.txt");
$x = <F>;
if ($x == "2"){
system "whatever";}

In c i've stuck with something like:

int main()
{
FILE *fp;
fp=fopen("file.txt","r");
char s[256];
while( fgets(s, sizeof(s),fp)!= NULL)
{
system("xterm");
}
fclose(fp);
return 0;
}

what i'm trying to do in my newbe eyes should look maybe like:

int main()
{
FILE *fp;
fp=fopen("file.txt","r");
char s[256];
if( fgets(s,fp)==1)
{
system("xterm");
}
fclose(fp);
return 0;
}

but i realy cannot find solution anywhere, so maybe someone can help me with that...

sytem() fn doesnt work for me either. My computer always says "Not enough memory" when I use system() & spawn...() functions. but then, many dos commands can be implemented using c. eg, you can manipulate files & folders, etc with some functions. Format a drive, and other dos functions using dos interrupt & ROM-BIOS interrupt if you are using a old C compiler like me.

Well your first C code was good.

Inside the while loop, do something like

if ( strncmp( s, "help", 4 ) == 0 ) {
  // do the help command.
}

Salem, thank you!:) the code:

int main()
{
FILE *fp;
fp=fopen("file.txt","r");
char s[256];
while( fgets(s, sizeof(s),fp)!= NULL)
if ( strncmp( s, "hi", 2 ) == 0 ){
{
system("xterm");
}}
fclose(fp);
return 0;
}

working perfect!
problem solved.

This article has been dead for over six months. Start a new discussion instead.