Reading file contents into an array of structures

I've created 2 structures. Now what Im trying to do is open these 2 txt files and write their contents to arrays of structures. So for instance, the Books.txt file has title, author, ISBN, availability, etc. for a few books and i am trying to write each item to the corresponding data variable into the "bookList" array of the "new" BookInfo data type structure.

The txt file im using is Books.txt and looks like this:

The Long Walk
King, Stephen
0-451-19671-6 34.00 1 -1
The Art of Fielding
Harbach, Chad
0-316-21753-0 28.77 1 -1
The Tiger's Wife
Obreht, Ted
0-385-34384-1 12.99 1 -1
Under the Dome
King, Stephen
1-4391-4903-4 34.90 0 123460



This is how I picture it in physical form:

bookList[1]                 bookList[2]               bookList[3]          ...etc   
bookList[1].title           bookList[2].title         bookList[3].title         
bookList[1].author          bookList[2].author        bookList[3].author     
bookList[1].ISBN            bookList[2].ISBN          bookList[3].ISBN       
etc
etc

and this is the code I have so far:

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

struct BookInfo
{
	string title;
	string author;
	string ISBN;
	float price;
	int availability;
	int borrowerId;
};

struct Member
{
	string FirstName;
	string LastName;
	int MemID;
	int BorrowStatus;
};

//****************************************
//*****prototypes*************************

void openFile(ifstream &fin);

//****************************************
//****************************************

int main()
{
	const int numBooks = 100;
	BookInfo bookList[numBooks];
	Member memberInfo[20];

	
	
	int count = 0;
	ifstream fin;
	

	openFile(fin);
	
	for(count = 0; count < numBooks; count++)
	{
		fin >> bookList[count].title ;
		fin >> bookList[count].author;
		fin >> bookList[count].ISBN;
		fin >> bookList[count].price;
		fin >> bookList[count].availability;
		fin >> bookList[count].borrowerId;	

			
	}	


	fin.close();

	for(count = 0; count < numBooks; count++)
		cout << bookList[count].title << endl;
		cout << bookList[count].author << endl;
		cout << bookList[count].ISBN << endl;
		cout << bookList[count].price << endl;
		cout << bookList[count].availability << endl;
		cout << bookList[count].borrowerId << endl;

	

return 0;
}

//*******************************************************
//******functions****************************************

void openFile(ifstream &fin)
{

string booklistFilename;

cout << "Please enter the filename for the booklist." <<endl;
	cin >> booklistFilename;
	fin.open(booklistFilename.c_str());
	if ( fin.fail())		
		cout <<"\nBad input file name, program terminated.\nPlease start over..\n\n\n";
}


any help would be appreciated!
Last edited on
closed account (DEUX92yv)
You might want to make openFile() an int or something; if it does happen to fail, it returns no indicator to main() that it failed, and main() will continue to run as if nothing happened. To solve this, and streamline your code, you can do away with this function altogether. Replace line 46 with lines 82-88, and add return 1; as the last line included in the if statement. Therefore, if opening the file fails, the program will exit.
(If you want the user to instead try again, use a while loop instead of the if - but if you choose to do this, make sure you prompt for a different file name!)

Does your program work at this point?
My teacher wants us to use functions as much as possible. If i do keep it as is, and a file does not open, it will still cout an error message so i want to keep that as it is. At this point it compiles but doesnt run correctly. Im wondering if i am using fin correctly when putting each structure element into the array. The output im getting is
The



































































































Segmentation fault
Last edited on
Topic archived. No new replies allowed.