我想编写一种简单的编程语言,但我无法解决所有这些错误!还要注意,我在Mac OS X上,下面的代码是我的所有代码。如果我能让这个工作起来,我会非常兴奋,如果我需要稍微编辑我的语言语法,我也没关系。我认为这是道路。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define buffersize 8192
int main(void) {
char buff[buffersize];
char filename[256];
scanf("%s", filename);
char rawfname[256];
strcpy(rawfname, filename);
int i;
for(i=0; filename[i]!='.'; i++);
while(rawfname[i]!='\0') {
rawfname[i] = '\0';
i++;
}
char fileloc[256] = "Macintosh HD/Users/user/c/";
char cfilename[256];
strcpy(cfilename, rawfname);
strcat(cfilename, ".c");
char cfileloc[256];
strcpy(cfileloc, fileloc);
strcat(cfileloc, rawfname);
strcat(cfileloc, ".c");
strcat(fileloc, filename);
FILE *fp = fopen(fileloc, "r");
if(fp != NULL) {
printf("Starting to compile...\n");
printf("Creating .c file...\n");
FILE *cfp;
cfp = fopen(cfilename, "w");
printf("Starting to create .c file..\n");
fputs("#include <stdio.h>\nint main(void) {\n", cfp);
printf("Started .c code...\n");
int line = 1;
printf("About to port to .c file...\n");
while(fgets(buff, buffersize, fp) != NULL) {
printf("Reading line %d", line);
if(strcmp(buff, "print:")) {
printf("Found print statment...\n");
fgets(buff, buffersize, fp);
line++;
printf("Reading line %d\n", line);
while(!strcmp(buff, "endprint")) {
char print[256] = " printf(";
strcat(print, (char*)22);
fputs(buff, cfp);
strcat(print, (char*)22);
strcat(print, ");\n");
fputs(print, cfp);
fgets(buff, buffersize, fp);
}
}
line++;
}
fputs(" return 0;\n}", cfp);
printf("Porting to .c complete...\n");
printf("About to compile..\n");
char compile[256];
strcpy(compile, "gcc ");
printf("Compile command started...\n");
strcat(compile, cfilename);
printf(".c file name added to compile command...\n");
strcat(compile, " -o ");
printf("' -o ' added to compile command...\n");
strcat(compile, rawfname);
printf("Executable file name added to compile command...\n");
system(compile);
printf("%s command executed...\n", compile);
printf("\nFile read and compile complete...\n");
} else {
printf("File could not be opened.\n");
}
fclose(fp);
}
这是我的程序的输出:
test.txt
Starting to compile...
Creating .c file...
Starting to create .c file..
Started .c code...
About to port to .c file...
Reading line 1
Found print statment...
Reading line 2
Found print statment...
Segmentation fault: 11
logout
的test.txt:
print:
It works!
endprint
并且test.c:
#include <stdio.h>
int main(void) {
return 0;
}
答案 0 :(得分:1)
if(!fp)
应该是
if(fp != NULL)
他们两个都不一样。