2017-01-15 8 views
-1
#define MAX_READING 100; 

char str_orders[MAX_READING], str_books[MAX_READING], 
    books_updated[MAX_READING], *token, *token1, p[MAX_READING], 
    File * books_orders, *books, books_updated; 

while (fgets (str_orders, MAX_READING, books_orders) != NULL) { 
    if (str_orders[strlen (str_orders) - 1] == '\n') 
     str_orders[(strlen (str_orders) - 1)] = 0; 
    if (strcmp (str_orders, "Initialize") == 0) { 
     while (fgets (str_books, MAX_READING, books) != NULL) { 
      if (str_books[strlen (str_books) - 1] == '\n') 
       str_books[(strlen (str_books) - 1)] = 0; 
      token = strtok (str_books, "$$$"); 
      strcpy (p, token); 
      token = strtok (NULL, "$$$"); 
      copy = atoi (token); 
      add (head, p, copy); 
     } 
    } 
    printf ("%s\n", str_orders); 
    if (strcmp (str_orders, "Initialize") != 0 
     && strcmp (str_orders, "Finalize") != 0) { 
     token1 = strtok (str_orders, "$$$"); 
     strcpy (order, token1); 
     token1 = strtok (NULL, "$$$"); 
     strcpy (book_name, token1); 
     token1 = strtok (NULL, "$$$"); 
     copy = atoi (token1); 
     if (strcmp (order, "Return") == 0) 
      returnbook (head, book_name, copy); 
     if (strcmp (order, "Borrow") == 0) 
      borrowbook (head, book_name, copy); 
     if (strcmp (str_orders, "Finalize") == 0) { 
      while (head != NULL) { 
       fprintf (books_update, "%s", head->name); 
       fprintf (books_update, " $$$ "); 
       fprintf (books_update, "%d", head->copies); 
      } 
     } 
    } 

私は、私はそれらを読むためにfgets機能を使用C. にtxtファイルから行ずつ読みしようとしているが、機能は「Intalize」と呼ばれる最初の行を読み取り、他の行に進みませんファイル内にあります。私はしようとしましたファイルから行を読み込む方法は?

printf("%s",str_orders) 

そしてそれは "Intalize"を返しています。 fgetsは行に進まなかった。

どうすれば修正できますか?

+4

の可能性のある重複このコードはコンパイルされません(http://stackoverflow.com/questions/3501338/c-read-file-line-by-line) – Unimportant

+3

を[Cが行毎にファイルを読みます]。程遠い。 [mcve]を入力してください。 –

答えて

0

ファイルを1行ずつ読み上げる方法は次のとおりです。

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

// Read a line from the file and put it into the buffer, and return 1 if we're at the end of the file 
int readLine(FILE *file, char *buffer, int bufferLength) { 
    int bufferPosition = 0; 

    while (true) { 
     // Copy the character into the buffer 
     buffer[bufferPosition] = fgetc(file); 

     if (feof(file)) { 
      // Return 1 if we're at the end of the file 
      buffer[bufferPosition] = 0; 
      return 1; 
     } else if (buffer[bufferPosition] == '\n') { 
      // Return if we're at the end of a line 
      buffer[bufferPosition] = 0; 
      return 0; 
     } else { 
      // Go to the next character 
      bufferPosition++; 
      if (bufferPosition == bufferLength) { 
       // If we fill up the buffer, exit here 
       buffer[bufferPosition - 1] = 0; 
       break; 
      } 
     } 
    } 

    return 0; 
} 

int main(int argc, char **argv) { 
    FILE *file = fopen("file.txt", "rb"); 

    int lineNumber = 1; 
    char line[4096]; 
    while (true) { 
     int read = readLine(file, line, sizeof(line)); 
     printf("Line %d: %s\n", lineNumber++, line); 
     if (read) break; // The end of the file 
    } 

    fclose(file); 
    return 0; 
} 
+0

readLine()はエラーを返しません。ファイルの終わりに達すると1を返します。このメソッドは、ファイルの改行文字で終わるかどうかわからないので、ファイルの最後の行にあるテキストがすべて出力されることを意味します。 – bace1000

+0

みんな!私は問題に投稿しました、私に答えてください:) –

+0

あなたのコードでは、 'fgets'を呼び出すたびに' str_orders'を上書きします。あなたは 'fgets'が各繰り返しを満たすために増やすことができるchar *へのポインタ*または*ポインタの配列を必要とします(例えば' str_orders [i ++] ') –

関連する問題