Thanks for reading this guys! I've got another c++ problem that's driving me crazy!
I'm trying to create a program that will read a text file and allow the user to search for a required string but the text file I have is causing me problems.
An example of the text file is as follows:
Forsyth, Frederick  ;The fist of god               ;A; 102391
Follett, Ken            ;The pillars of the earth   ;A; 103795
Francis, Clare         ;Wolf winter                   ;A; 112424
Smith, Wilbur          ;A falcon flies                ;A; 151395
Forsyth, Frederick   ;The day of the jackal    ;A; 159151
Blackmore, R.D.      ;Lorna Doone                ;A; 193292
my code so far is:
#include %26lt;iostream%26gt;
#include %26lt;fstream%26gt;
#include %26lt;stdlib.h%26gt;
#include %26lt;string%26gt;
using namespace std; 
      int main()
   
      {
   
      string BookAuthSurName;
      string BookAuthFirstName;
      string BookTitle;
      string BookIDcode;
   
       
   
      ifstream bookdata;
   
      bookdata.open("bookdata.txt");
   
   
    if (!bookdata)
      {
   cout %26lt;%26lt; "File not found." %26lt;%26lt; endl;
      }
   
      {
      while (!bookdata.eof() )
      {
 
      for(int i = 0; !bookdata.eof(); i++)
      {
      getline(bookdata, BookAuthSurName, ' ' );
      getline(bookdata, BookAuthFirstName, ' ');
      getline(bookdata, BookTitle, ' ');
      getline(bookdata, BookIDcode, '\n');
       
      cout%26lt;%26lt;"BookAuthSurName: "%26lt;%26lt;BookAuthSurName%26lt;%26lt;endl;
      cout%26lt;%26lt;"BookAuthFirstName: "%26lt;%26lt;BookAuthFirstName%26lt;%26lt;endl;
      cout%26lt;%26lt;"BookTitle: "%26lt;%26lt;BookTitle%26lt;%26lt;endl;
      cout%26lt;%26lt;"BookIDcode: "%26lt;%26lt;BookIDcode%26lt;%26lt;endl;
      cout%26lt;%26lt;"---------------------------------鈥?br>
      }
      }
      }
      bookdata.close();
      }
an example of the output is:
BookAuthSurName: Forsyth,
BookAuthFirstName: Frederick
BookTitle: 
BookIDcode:     ;The fist of god               ;A; 102391
-------------------------------------
BookAuthSurName: Follett,
BookAuthFirstName: Ken
BookTitle: 
BookIDcode:           ;The pillars of the earth      ;A; 103795
-------------------------------------
BookAuthSurName: Francis,
BookAuthFirstName: Clare
BookTitle: 
BookIDcode:         ;Wolf winter                   ;A; 112424
-------------------------------------
Now what I cant work out is how to break the data up into variables and put it in a struct?
I am assuing that once the data is in a struct I will be able to perform something like a linear search on each line to find the data?
if anyone could shed any light on these problems I would be most grateful!How can I search a text file in C++?
So you will use a struct to define a new type that stores a collection of information on books. A good type name would be "Book."
We can define it as:
struct Book
{
  String member1;
  String member2;
  ...
  String memberN;
}
You will populate a struct array or vector depending on what you choose.
Then you just iterate doing a substring scan of what you're trying to find. See source for more information.How can I search a text file in C++?
your struct would look something like this:
struct Book
{
string BookAuthSurName;
string BookAuthFirstName;
string BookTitle;
string BookIDcode;
}
Then make an aray of Book called Library (or whatever you want):
Book Library[20];
Note: There are other ways to do it, but for ease of coding I just declared an array of 20 Book's.
Then to load the array:
i = 0;
do{
getline(bookdata, Library[i].BookAuthSurName, ' ' );
getline(bookdata, Library[i].BookAuthFirstName, ' ');
getline(bookdata, Library[i].BookTitle, ' ');
getline(bookdata, Library[i].BookIDcode, '\n');
i++;
}
while (!bookdata.eof());
Subscribe to:
Post Comments (Atom)
 
No comments:
Post a Comment