2017-02-24 35 views
2

我有一個代碼,我想檢查文件中的行數。該文件有6行。我嘗試與新行'/ n'比較,但結果不好。這是代碼。請幫助:)用新行C比較字符

#include <stdio.h> 
#include <stdlib.h> 
int main() 
{ 
    FILE*code; 
    int NumbRow = 0; 
    char c; 

    code = fopen("SMS.txt", "r"); 
    if(code==NULL) 
    { 
     printf("I cant read from file"); 
     return; 
    } 
again: 
    while ((c=fgetc(code)) !=EOF) 
    { 
     if (c=='/n') 
     { 
      NumRow ++ ; 
     } 
     if (c!='/n') 
     { 
      goto again; 
     } 
    } 
    printf ("Number of rows is : %d", NumRow); 
    return 0; 
} 
+0

嗯。 「但結果不好」不是一個非常準確的問題陳述。 –

+0

goto是什麼? – pinkfloydx33

回答

2

使用'\n'而不是'/n'在你的代碼。 '\n'將您移到下一行。

+3

...並檢查您的編譯器是否可以警告具有多個字符的字符文字(「/ n''有兩個,但'\ n''只有一個,單個換行符)。 – unwind

0

你必須使用'\n'作爲一個換行符,但使用的是'/n'這不是一個換行符'/n'意思是有兩個字符的'/''n'