-2
#include <stdio.h>
#define MAX_TITLE_SIZE 20
#define MAX_BOOKS 10

struct Book {
    int _isbn;
    float _price;
    int _year;
    char _title[MAX_TITLE_SIZE + 1];
    int _qty;
};

void clear(void);
int readRecord(FILE *fp, struct Book *b2read);
void displayInventory(const char filename[]);



int main(void) {

  struct Book myBook;
  char filename[21] = "144_w9_inventory.txt";

  displayInventory(filename);

    return 0;
}

void clear(void) {

    while (getchar() != '\n');

}

int readRecord(FILE *fp, struct Book *b2read){

    //Define a variable int rv = 0
    int rv = 0;


    rv = fscanf(fp, "%d;%f;%d;%d;%20[^\n]", &(b2read->_isbn), &(b2read->_price), &(b2read->_year), &(b2read->_qty), b2read->_title);
    //return rv;

    return rv;
}

void displayInventory(const char filename[]) {

    struct Book myBook;

    FILE *fp = NULL;
    int i;

    fp = fopen(filename, "r");  //open the file for reading

    if (fp != NULL) {

        printf("\n\nInventory\n");
        printf("===================================================\n");
        printf("ISBN      Title               Year Price  Quantity\n");
        printf("---------+-------------------+----+-------+--------\n");

        while(readRecord(fp, &myBook) == 5){
         printf("%-10.0d%-20s%-5d$%-8.2f%-8d\n", myBook._isbn, myBook._title, myBook._year, myBook._price, myBook._qty); 
        }
        printf("===================================================\n");
        printf("\n");

        fclose(fp);
    }
    else {
        printf("Failed to open file\n");
    }
}

What is inside the text file is:

234562;23.99;2010;3;Harry Potter
567890;12.67;2015;4;The Hunger Games
109821;53.20;2017;2;Stranger Things

The Output:

Inventory
===================================================
ISBN      Title               Year Price  Quantity
---------+-------------------+----+-------+--------
234562    Harry Potter
       2010 $23.99   3       
567890    The Hunger Games
   2015 $12.67   4       
109821    Stranger Things     2017 $53.20   2       
===================================================

When I output the program I'm able to get all the values but for some reason when I print those values the whole string gets halved and shifted down a line.

The repl.it is here if you want to take a look:

https://repl.it/JbRy/69

How do I get the output to print out in single lines; rather than reading \n "newlines" if that's the case?

mincedMinx
  • 159
  • 1
  • 3
  • 12
  • 1
    This code looks fine, and works as expected on my system. Can't reproduce; are you sure that this doesn't have something to do with the online test environment you are running the code in? – ad absurdum Jul 21 '17 at 01:52
  • You're right @DavidBowling, it worked on Visual Studio but it compiled differently in the repl.it environment. I use repl.it to test my outputs instead of having to create a new instance of VS everytime I want to test something. – mincedMinx Jul 21 '17 at 16:14

3 Answers3

-1

The difference between 'Harry Potter' and 'Stranger Things' is that 'Stranger Things' has no line breaks in the text file. 'Harry Potter' and 'The Hunger Games' seem to have '\r' left.

Try this.

rv = fscanf(fp, "%d;%f;%d;%d;%20[^\r\n]", &(b2read->_isbn), &(b2read->_price), &(b2read->_year), &(b2read->_qty), b2read->_title);
gwangsoo
  • 9
  • 2
  • `\r` is not a line break. – too honest for this site Jul 21 '17 at 03:07
  • Difficult to see how this would explain OP's problem. If newlines are `\r\n` in the input file, then the missed `\r` would cause the cursor to move to the beginning of the current line, not advance to the beginning of the next line. Something seems fishy with either the input file, the online testing environment, or both.... – ad absurdum Jul 21 '17 at 05:49
-1

It has nothing to do with 'clearing the input buffer'. fscanf() consumes it according to what you specify. It doesn't need 'clearing', it needs scanning correctly. Somehow you are geting the line terminator in the title. Correct your fscanf() format string.

user207421
  • 305,947
  • 44
  • 307
  • 483
-1

There's a little thing missing when you fscanf. You should include \r in [^\n]. This will tell fscanf that you want to stop reading when either carriage return or line feed are encountered.

Like this:

rv = fscanf(fp, "%d;%f;%d;%d;%20[^\n\r]", &(b2read->_isbn), &(b2read->_price), &(b2read->_year), &(b2read->_qty), b2read->_title);