C语言使用Shell实例源码:
#include <stdio.h>
#include <stdlib.h>
void write_file()
{
FILE *fp;
char *name = “hello.c”;
char *mode = “w”;
fp = fopen(name, mode);
if (fp == NULL)
{
printf(“can’t open %s\n”, name);
exit(1);
}
fprintf(fp, “#include<stdio.h>\n”);
fprintf(fp, “int main(int argc, char *argv[])\n”);
fprintf(fp, “{\n”);
fprintf(fp, “printf(\”hello boy!\\n\”);\n”);
fprintf(fp, “return 0;\n”);
fprintf(fp, “}\n”);
fclose(fp);
}
void command(char *str)
{
int result = system(str);
if (result != 0) exit(result);
}
int main(int argc, char *argv[])
{
write_file();
command(“gcc -Wall hello.c -o hello”);
command(“./hello”);
return 0;
}