C++ program
Hi I need help creating a function that can read data from a file (using fstream and file IO) into a data structure (struct). Here is the sample struct.
const int STR_SIZE = 51;
struct bookType
{ char booktitle[STR_SIZE]; char isbn[STR_SIZE]; char author[STR_SIZE]; char publisher[STR_SIZE]; char dateAdded[STR_SIZE]; int qtyOnHand; double wholesale; double retail; };
Here is the sample data: Star Wars 0345260791 George Lucas Del Rey 10/18/2017 59.95 100.00 The Empire Strikes Back (Star Wars) 034529209X George Lucas Ballantine 04/01/2017 179.58 185.00
Hey there!
I have understood your query and made the required program.
Say you have a text file named input.txt which contains the input you want to take in. For that, make sure that this "input.txt" file is present in the same directory/folder as this program.cpp
Along with the text code, i'll share the screenshots of the code (because it looks more clean and presentable). And also the input and output.
Here's the code (C++)
//----------------------------------------------------//
//
PROGRAM STARTS HERE
//
//----------------------------------------------------//
#include <iostream>
#include <fstream>
using namespace std;
const int STR_SIZE = 51;
struct bookType {
char booktitle[STR_SIZE];
char isbn[STR_SIZE];
char author[STR_SIZE];
char publisher[STR_SIZE];
char dateAdded[STR_SIZE];
int qtyOnHand;
double wholesale;
double retail;
};
// Here we passed the bookType object by reference because we
wanted that whatever
// input goes into this object should reflect in the object in the
main function.
// If we would have passed it by value, then this function would
have made a copy
// of the bookType object and take input in the object (copy
object) but the object
// in the main would have remained intact. No input would have been
taken by the
// object in the main() function.
// Hence we passed our object by reference.
void takeInput(bookType &bt){
// Initializing an object of ifstream class
ifstream fin;
// opening the input file named "input.txt" which
must be located
// in the same directory/folder as this program
fin.open("input.txt");
// Now the trick to use file objects is very
Simple!
// Just treat them like your regular cin/cout and go
on.
// so here, our cin becomes fin, that's it.
// Taking all the input from the input file.
fin.getline(bt.booktitle, STR_SIZE);
fin.getline(bt.isbn, STR_SIZE);
fin.getline(bt.author, STR_SIZE);
fin.getline(bt.publisher, STR_SIZE);
fin.getline(bt.dateAdded, STR_SIZE);
fin >> bt.qtyOnHand;
fin >> bt.wholesale;
fin >> bt.retail;
}
void showBookType(bookType bt){
cout << "Book title\t: " <<
bt.booktitle << endl;
cout << "ISBN\t\t: " << bt.isbn <<
endl;
cout << "Author\t\t: " << bt.author
<< endl;
cout << "Publisher\t: " << bt.publisher
<< endl;
cout << "Date Added\t: " << bt.dateAdded
<< endl;
cout << "Quantity\t: " << bt.qtyOnHand
<< endl;
cout << "Wholesale\t: " << bt.wholesale
<< endl;
cout << "Retail\t\t: " << bt.retail
<< endl;
}
int main(){
// declaring an object of bookType
bookType bt;
// taking input in the instance of bookType
takeInput(bt);
// print the input taken in the instance of
bookType
showBookType(bt);
}
//----------------------------------------------------//
//
PROGRAM ENDS HERE
//
//----------------------------------------------------//
Here's the screenshots of the code:



Input: (input.txt)

Output:

I hope I was able to answer your query. Feel free to ask doubts in the comment section. :)
C++ program Hi I need help creating a function that can read data from a file...