C segfault 11以及文件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]!='') {
        rawfname[i] = '';
        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 %dn", 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

和测试。c:

#include <stdio.h>
int main(void) {
 return 0;
}
if(!fp)

应该是

if(fp != NULL)

它们都不一样。

相关内容

  • 没有找到相关文章

最新更新