C 段错误 11 和文件 i/o 可能的其他错误
C segfault 11 and possibly other errors with file i/o
我想编写一种简单的编程语言,但我无法克服所有这些错误!另外要注意的是,我在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]!='[=11=]') {
rawfname[i] = '[=11=]';
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;
}
if(!fp)
应该是
if(fp != NULL)
两者不一样
我想编写一种简单的编程语言,但我无法克服所有这些错误!另外要注意的是,我在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]!='[=11=]') {
rawfname[i] = '[=11=]';
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;
}
if(!fp)
应该是
if(fp != NULL)
两者不一样