2017-01-03 115 views
-4

但我在運行此程序時不斷收到此錯誤。我認爲這是因爲fgets功能。我嘗試初始化輸入變量爲NULL,看看是否會有所幫助,但事實並非如此。我也有一種預感,我可能需要malloc來解決問題。但你的幫助是高度讚賞。由於fgets導致的分段錯誤(核心轉儲) - 我認爲

int main(int argc, char* argv[]) 
{ 
    char* input = NULL; 

    // ensure one and only one command line argument 
    if (argc != 2) 
    { 
     printf("Usage: %s [name of document]\n", argv[0]); 
     return 1; 
    } 

    // open a new document for writing 
    FILE* fp = fopen(argv[1], "w"); 

    // check for successful open 
    if(fp == NULL) 
    { 
     printf("Could not create %s\n", argv[1]); 
     return 2; 
    } 

    // get text from user and save to file 
    while(true) 
    { 
     // get text from user 
     printf("Enter a new line of text (or \"quit\"):\n"); 
     fgets(input, 50, stdin); 

     // if user wants to quit 
     if (input != NULL && strcmp(input, "quit") == 0) 
     { 
      free(input); 
      break; 
     } 
     // if user wants to enter text 
     else if (input != NULL) 
     { 
      fputs(input, fp); 
      fputs("\n", fp); 
      printf("CHA-CHING!\n\n"); 
      free(input); 
     } 
    } 

    // close the file and end successfuly 
    fclose(fp); 
    return 0; 
} 
+0

您只能在'malloc'和朋友分配的內存中使用'free'。您在這裏沒有分配任何內存。 –

+2

@DavidBowling:'free'實際上是合法的,儘管缺少'malloc',因爲'NULL'可以安全地'免費',但它不是很有用。 – ShadowRanger

+0

查看[fgets(3)'如何工作]手冊(https://linux.die.net/man/3/fgets)。將一個NULL指針傳遞給'fgets(3)'確實是錯誤的和意外的。作爲您未來工作的一般建議:*不要只是使用某些功能,而無需閱讀其文檔以瞭解它的工作原理。 – giusti

回答

0

雖然您可以在這裏使用malloc(),但並不是真的有必要。您可以#define合理的最大行長度,並聲明一個字符數組來保存輸入。如果你這樣做,你可以從你的代碼中刪除free

對於您使用的方式fgets()也存在問題。尾隨\nfgets()保留,但您的比較忽略了這一點。因此,input永遠不會等於"quit",肯定不會是NULL。我已經包含了一些代碼,在讀入input後刪除了尾隨的換行符;該代碼還會清除輸入流中的所有剩餘字符,如果用戶輸入多於MAXLINE - 1個字符,則可能會出現這種情況。文本輸入的測試只是簡單的if (input[0])。或者,您可以更改測試以考慮額外的'\ n'字符。

#include <stdio.h> 
#include <string.h> 
#include <stdbool.h> 

#define MAXLINE 1000 

int main(int argc, char* argv[]) 
{ 
    char input[MAXLINE]; 
    char *ch;      // used to remove newline 
    char c;       // used to clear input stream 

    // ensure one and only one command line argument 
    if (argc != 2) 
    { 
     printf("Usage: %s [name of document]\n", argv[0]); 
     return 1; 
    } 

    // open a new document for writing 
    FILE* fp = fopen(argv[1], "w"); 

    // check for successful open 
    if(fp == NULL) 
    { 
     printf("Could not create %s\n", argv[1]); 
     return 2; 
    } 

    // get text from user and save to file 
    while(true) 
    { 
     // get text from user 
     printf("Enter a new line of text (or \"quit\"):\n"); 
     fgets(input, MAXLINE, stdin); 

     // remove trailing newline 
     ch = input; 
     while (*ch != '\n' && *ch != '\0') { 
      ++ch; 
     } 
     if (*ch) { 
      *ch = '\0'; 
     } else {   // remove any extra characters in input stream 
      while ((c = getchar()) != '\n' && c != EOF) 
       continue; 
     } 

     // if user wants to quit 
     if (strcmp(input, "quit") == 0) 
     { 
      break; 
     } 

     // if user wants to enter text 
     else if (input[0]) 
     { 
      fputs(input, fp); 
      fputs("\n", fp); 
      printf("CHA-CHING!\n\n"); 
     } 
    } 

    // close the file and end successfuly 
    fclose(fp); 
    return 0; 
} 
+0

感謝您的時間,非常感謝! –

1

你永遠不malloc -ed input,所以是的,fgets被提領該NULL指針作爲緩衝,而這就要死了。將input更改爲堆棧陣列(並刪除free)或實際調用malloc來分配內存,因此input未指向NULL

0

我認爲這是因爲fgets函數。

是:傳遞NULL指針fgets使得沒有意義,是不允許的,並會導致崩潰。

我可能需要malloc來解決問題。

您需要將指針傳遞到一個合適的緩衝器fgets讀取輸入到。無論該緩衝區是malloc ed,本地還是全局陣列,都是不相關的

TL; DR:認爲關於你在做什麼。

+0

感謝您的建議.... –

0

他們是你的代碼中的一些問題。

  1. 您還沒有分配內存來輸入字符指針。因此你不能在其中存儲字符,因此你會得到分段錯誤。

  2. 你也是免費不止一次,這是不正確的。

所以,一個代碼,與上述修改將是這樣的:

#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 

int main(int argc, char* argv[]) 
{ 
     char* input = malloc(sizeof(char) * 50); 

     // ensure one and only one command line argument 
     if (argc != 2) 
     { 
      printf("Usage: %s [name of document]\n", argv[0]); 
      return 1; 
     } 

     // open a new document for writing 
     FILE* fp = fopen(argv[1], "w"); 

     // check for successful open 
     if(fp == NULL) 
     { 
      printf("Could not create %s\n", argv[1]); 
      return 2; 
     } 

     // get text from user and save to file 
     while(1) 
     { 
      // get text from user 
      printf("Enter a new line of text (or \"quit\"):\n"); 
      fgets(input, 50, stdin); 

      // if user wants to quit 
      if (input != NULL && strcmp(input, "quit\n") == 0) 
      { 
       free(input); 
       break; 
      } 
      // if user wants to enter text 
      else if (input != NULL) 
      { 
       fputs(input, fp); 
       fputs("\n", fp); 
       printf("CHA-CHING!\n\n"); 
       // free(input); 
      } 
     } 

     // close the file and end successfuly 
     fclose(fp); 
     return 0; 
} 

希望它可以幫助你的問題。 乾杯。

+0

我會感謝您的幫助,感謝您的時間! –

+0

@IrisSunmola,如果你接受有助於你解決問題的答案會更好。當他們看這篇文章時,這會節省其他人的時間。 –