2013-03-09 71 views
-1
#define String char* 
#define FileP FILE* 
#define null ((void*) 0) 
#include "defs.h" 

int main(int argc, char** argv) { 

    Stack stack; 
    init(&stack); 

    FileP file = readp("Props.props"); 

    if (file == null){ 
     printf("Unable to Load\n"); 
     exit(1); 
    } 

    String buffer; 
    int m = 0; 
    char ch; 

    while (!feof(file)) { 
     ch = getc(file); 
     if (ch != ' ') { 
      *(buffer + (m++)) = ch; 
     } else { 
      push(&stack, buffer); 
      m = 0; 
     } 
    } 

    int i; 

    for (i = 0; i < MAX_SIZE; i++) { 
     printf("%s\n", pop(&stack)); 
    } 

    fclose(file); 

    return 0; 

} 

defs.h包含所有的#defines,但我將它們包含在這裏,讓你知道它們是什麼。該程序打開一個名爲「props.props」的文本文件並讀取每個字符串,然後將它們存儲到堆棧中,最後打印出堆棧。運行時沒有任何反應,只是由於運行時錯誤而退出。這是爲什麼發生?這個程序爲什麼會有運行時錯誤?

+0

究竟是哪個運行時錯誤向您顯示? – SRJ 2013-03-09 03:27:05

+0

它沒有顯示我,它只是表示運行時錯誤 – Richard 2013-03-09 03:30:59

+0

請提供像readp()等功能的定義...它很難從此代碼跟蹤。 – SRJ 2013-03-09 03:34:23

回答

1

buffer未初始化。您需要分配內存

buffer = (char*) malloc(n * sizeof (char)); 

其中n是您需要存儲的字符數。 此外,該文件似乎並未在任何地方打開(fopen)。什麼是readp

+0

readp在其中有fopen方法,謝謝,我通常有這樣的錯誤,非常明顯。 – Richard 2013-03-09 03:20:46

相關問題