2017-07-22 7 views
1

Dies ist ein Programm, das ein Inventar von Büchern aus einer Textdatei lesen soll (jede Zeile der Textdatei enthält ein anderes Feld in der richtigen Reihenfolge für jedes Buch)). Aus irgendeinem Grund wird die Routine unendlich fortgesetzt. Die "eof" -Fehlerüberfüllung scheint das Ende der Datei nicht zu erfassen. Kann jemand mir eine Idee geben, was falsch ist? Bitte denken Sie daran, ich bin ein echter Neuling in der C++ Welt, aber kein Neuling für die Programmierung.Lesen von Daten aus einer Textdatei in eine C++ - Struktur

Zuerst wird die Struktur Definition für Buch

// structure definition 
struct Book 
{ string isbn; 
    string title; 
    string author; 
    string publisher; 
    int quantity; 
    float price; }; 

ich die richtigen Header-Dateien enthalten sind. Unten ist die Routine, die scheinbar nicht funktioniert. Ich habe einen Cout eingefügt < < "machte es in Linie 1" endl; Code, um den Fortschritt der Routine zu sehen, und wiederholt einfach das Abrufen von Daten aus der Textdatei. Die Textdatei enthält nur 6 Bücher, daher sollte das Lesen der Textdatei ziemlich schnell beendet werden.

Vielen Dank, bitte, für jede Hilfe!

void readInventory(string fileName, vector<Book>& inventory) 
{ 
    ifstream instream; 
    // tries to open the file 
    instream.open(fileName); 

    // checkes if the file is open 
    if (instream) 
    { 
     // string line; 
     // reads through the file line by line 
     while (!instream.eof()) 
     { 
      Book newBook; 

      // read book from text file 
      instream >> newBook.isbn; 
      instream >> newBook.title; 
      instream >> newBook.author; 
      instream >> newBook.publisher; 
      instream >> newBook.quantity; 
      instream >> newBook.price; 

      // add this book to inventory 
      inventory.push_back(newBook); 
     } 

     instream.close(); 
     showMessage(to_string(inventory.size()) + " books were successfully added to inventory"); 
    } 

    // if failed to open the file 
    else 
     showMessage("Cannot open the file. Make sure the file name is correct"); 
} 
+2

Problem 1. 'instream.eof()' -Siehe https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong. PS: Entfernen Sie die Lebensgeschichte am Anfang - es ist nicht das intereting –

+1

Sind alle Daten von struct in Datei in einer neuen Zeile, isbn, Titel sind die gleiche Zeile? –

+0

Überprüfen Sie diese Frage auch https://stackoverflow.com/questions/6512173/ifstream-not-reading-eof-character –

Antwort

0

Sie können Programm so schreiben.

#include <iostream> 
#include <vector> 
#include <algorithm> 
#include <string> 
#include <cmath> 
#include <set> 
#include <queue> 
#include <fstream> 
#include <sstream> 

using namespace std; 
// structure definition 
struct Book 
{ string isbn; 
    string title; 
    string author; 
    string publisher; 
    int quantity; 
    float price; 

}; 

void fillBook(const std::string& line,Book& book) 
{ 
    std::stringstream is(line); 
    std::string tempStr; 
    short cnt = 0; 

    while(std::getline(is, tempStr, ' ')) 
    { 
     if(cnt ==0) 
      book.isbn =tempStr; 
     else if(cnt ==1) 
      book.title = tempStr; 
     else if(cnt ==2) 
      book.author = tempStr; 
     else if(cnt ==3) 
      book.publisher= tempStr; 
     else if(cnt ==4) 
      book.quantity = atoi(tempStr.c_str()); 
     else if(cnt == 5) 
      book.price = atof(tempStr.c_str()); 

     ++cnt; 
    } 

} 

void readInventory(string fileName, vector<Book>& inventory) 
{ 
    ifstream instream; 
    // tries to open the file 
    instream.open(fileName); 

    // checkes if the file is open 
    if (instream) 
    { 
     // string line; 
     // reads through the file line by line 
     std::string line; 
     while (std::getline(instream, line, '\n')) 
     { 
      Book newBook; 

      fillBook(line,newBook); 

      // add this book to inventory 
      inventory.push_back(newBook); 
     } 

     instream.close(); 
     std::cout<<(to_string(inventory.size()) + " books were successfully added to inventory"); 
    } 

    // if failed to open the file 
    else 
     std::cout<<("Cannot open the file. Make sure the file name is correct"); 
} 
+0

Vielen Dank, mystic_coder. Dein Code hat gut funktioniert! –

Verwandte Themen