Reading file into a class array

For my homework assignment, I have to create a class titled "Book" and I need to read a file, of a max size of 2000 lines, line by line into the class type below and then sort each line into alphabetical order. However, I don't know how to approach reading the file into the array. I have posted what I have so far below and the input file. I also wouldn't mind any suggestions when it comes to outputting the array after it is sorted.

The input file would look something like this:
000001, Bjarne Stroustrup, The C++ Programming Language, 4, 2013, 0321563840
000002, Stephen Colbert, Stephen Colbert's Midnight Confessions, 1, 2017, 1501169009
000003, Tom Clancy, The Hunt for Red October, 2, 1984, 0425240339
000004, Gregory Dudek, Computational Principles of Mobile Robotics, 2, 2010, 0521692121

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
 #include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;

  class Book
{
private:
  string ID;
  string author;
  string title;
  int edition;
  int year;
  string ISBN;
public:
    Book ()
  {
    ID = "";
    author = "";
    title = "";
    edition = 0;
    year = 1900;
    ISBN = "";
  }
  Book (string id, string a, string t, int edt, int yr, string isbn)
  {
    ID = id;
    author = a;
    title = t;
    edition = edt;
    year = yr;
    ISBN = isbn;
  }
};

int main ()
{
   int main ()
{

  Book bookist[2000];
  int size;
  string temp;
  ifstream input;
  input.open ("bookinput.dat");
  if (input.fail)
    {
      cout << "Error when opening file" << endl;
      exit (1);
    }
  ofstream out;
  out.open ("bookouput.dat") if (out.fail)
    {
      cout << "Error when opening output file" << endl;
      exit (1);
    }
    (while getline(input, str)) {
        size++;
    }
    for(int i = 0; i < size; i++) {
        in >> booklist[i];
// I know this doesn't work but you get the idea
    }
}
The first thing I see is two main() functions, one inside the other. There can only be one main() and you can't implement one function inside another function.

Second there is no need to use the C function exit() in this function, just return(1); instead.

Third, you read each line to "count" the number of lines, this is not necessary and it will cause problems because that first read puts your file stream into an error state so no further processing is possible. Just get rid of the first read altogether.

Fourth, until you overload the operator>> for your class you can't just read the file in one swoop.

You would probably be better off reading a line at a time and then use a stringstream to parse that line. You may also want to consider creating the overloaded operator>> to make things easier as well.

And while we're on the subject of a line make sure your file doesn't have any whitespace characters in front of or behind the delimiter as this could cause problems.

000001,Bjarne Stroustrup,The C++ Programming Language,4,2013,0321563840

Lastly, for now, your read loop should look something like:
1
2
3
4
5
6
7
size_t size = 0;
while(size < MAX_ARRAY_SIZE && getlline(input, str))
{
    ... Process the line here.

    ... increment size here.
} // End of read loop. 


Topic archived. No new replies allowed.