如何从文件中逐行阅读?

时间:2017-01-15 10:14:02

标签: c file-io readfile

#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);
            }
        }
    }

我正试图从C中的txt文件中逐行读取。 我使用fgets函数来读取它们,但该函数读取名为“Intalize”的第一行,不会继续执行文件中的其他行。我试着

printf("%s",str_orders)

它正在返回“Intalize”。 fgets没有进入排队。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

这是我如何逐行阅读文件。

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