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

I'm trying to read line by line from a txt file in C. I used the fgets function to read them, but the function reads the first line called "Intalize", doesn't proceed to other lines in the file. I tried to

printf("%s",str_orders)

and it's returning "Intalize". The fgets didn't proceed to the lines.

How can I fix that?

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85

1 Answers1

0

Here's how I would go about reading a file line-by-line.

#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;
}
  • readLine() doesn't return an error. It returns 1 if the end of the file has been reached. The method means any text on the last line of the file will be printed, since we don't know if the file ends with a newline character. –  Jan 15 '17 at 10:49
  • guys ! I that have posted to problem, please answer me :) – Karem Wattad Jan 15 '17 at 11:50
  • In your code, you overwrite `str_orders` on every call to `fgets`. You need an *array of strings* or a *pointer to pointer to char* that you can increment to provide a new address for `fgets` to fill on each iteration (e.g `str_orders[i++]`) – David C. Rankin Jan 16 '17 at 09:17