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
    }
}
closed account (NCRLwA7f)
Do you want to separate the data into each individual field or do you want to read in the whole line. Also, does your file have comas or did you add those, it’s easier if you dont have any comas. Also is each book going to have the same amount of data, for example isbn number, year etc..
I need to separate the data into each individual field (ID, author, title, etc.) and unfortunately, the file has comas which probably my primary issue with figuring this out. As for the amount of data, each ID should have the same amount of data, same goes for the ISBN and the year. With the author, title, and edition having varying amounts of characters/integers.
closed account (NCRLwA7f)
ok so what you need to use is getline()

the way getline works is by reading the whole line of a document, but also you can use a delimiter, what this does is to stop reading until you reach a particular character, in this case, a coma. The delimiter also reads blank spaces so you have to be careful with that as well.
getline reads in a string, so you will not be able to do a getline function for an int
this means that if you want to use a number you will have to convert it from a string to an integer. From the looks of it, you will probably just use the data as strings

1
2
3
4
5
6
7
8
9
10
11
12
13

#include <string> // to use getline


int main(){

string myData;

getline(input, myData, ',' ); //this should bring in a string up to the coma

//there will probably be a blank space after the coma and if you do getline again //it might not bring in anything so you will probably have to do
input.ignore;   //that should ignore the blank and put you at the start of the next string to bring in.   


There a couple of ways you can bring your data in.

one is to do

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

getline(input, myData, ',' );

ID = myData;

getline(input, myData, ',' );

author = myData;

//etc..... you can put this in a loop


or you can do something like

getline(input, ID, ',' );
getline(input, author, ',');

//etc...you can put this in a loop

return 0;

}



Also, what will you be using to store your data, I know its a class, but are you using a linked list, array, queue, stack...?
Last edited on
I intended on using an array
closed account (NCRLwA7f)
since you have various types of data for a single book I would suggest using a struct. you can still use it similar to an Array in the sense that you can do

book[i]


check out this link for some examples, it is very close to what you are doing, except that this one is movie titles.
http://www.cplusplus.com/doc/tutorial/structures/

since you are doing a class, you can use the structure inside if you like

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
66
67
68
69

class  book{

      
       private: // you might have to make the struct public:
                   //if you leave it private, you will need to make a separate public function to access the private data in the struct
       struct   bookRec{
             string ID;
             string author;
             string title;
             int edition;
             int year;
             string ISBN;
       } book[i] ;  // where i is the number of books you have

       

       public:
             Book (int i)
            {
                  book[i].ID = "";
                  book[i].author = "";
                  book[i].title = "";
                  book[i].edition = 0;
                  book[i].year = 1900;
                 book[i].ISBN = "";
            }
          Book (string id, string a, string t, int edt, int yr, string isbn, int i)
         {
                 book[i].ID = id;
                 book[i].author = a;
                 book[i].title = t;
                 book[i].edition = edt;
                book[i]. year = yr;
                book[i]. ISBN = isbn;
          }

};



int main(){
  ifstream input;
  input.open ("bookinput.dat");

  ofstream out;
  out.open ("bookouput.dat");

  //do your file check


   Book booklist;

//you can read in the data and fill each field for each record
for(int i = 0; i < size ; i++){
   booklist.book[i].ID;
   booklist.book[i].ISBN;
   booklist.book[i].author;
   //etc...etc...
}






   return 0;
}



this code is not tested so don't expect it to run right out the box.

hope it helps
Topic archived. No new replies allowed.