2015-02-11 33 views
-1

我想寫一個簡單的編程語言,但我無法克服所有這些錯誤!另外要注意的是,我在Mac OS X上,下面的代碼是我擁有的所有代碼。如果我能夠實現這個目標,我會非常激動,如果我需要編輯我的語言的語法,我很好。我認爲這是道路。C段錯誤11和可能的其他錯誤與文件I/O

#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; 
} 
+1

'fp'是一個空指針(因爲'if(!fp){')。 'strcat(print,(char *)22);'看起來錯了。 – cremno 2015-02-11 04:17:53

+1

如何使用調試器而不是將代碼粘貼到此處供其他人爲您調試? – hyde 2015-02-11 04:23:13

+0

另外,你有幾個調用(至少有'fgets','fopen'),你不檢查錯誤。 – hyde 2015-02-11 04:24:54

回答

1
if(!fp) 

應該是

if(fp != NULL) 

它們都不相同。

+0

這有幫助,但現在它不是打開文本文件。我認爲這是道路。 – 2015-02-11 15:10:45

+0

@TucowUncow是的,請檢查文件路徑和文件名 – Gopi 2015-02-11 15:12:33

+0

我會馬上。 – 2015-02-11 15:17:56