Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I have a Books.txt file which looks like this.
Book name---------------Author
Lord of the rings---------J. R R Tolkiens
Tom sawyer--------------Mark twain
Just after sunset---------Stephen king
The shining--------------Stephen king
Oliver twist------------Charles Dickens
Huck Finn---------------Marktwain
Children  on Hurin------J R R Tolkiens
Silmarilions------------J R R tolkiens
How can i make my c++ program to read this file, read records as arrays, sort them, and search either by book name or by the auhtor. If i write Tolkiends, how can i make it output all the books written by J R R tolkiens?
Posted 12-May-12 1:24am
Edited 12-May-12 1:25am
v2
Comments
Sandeep Mewara at 12-May-12 8:35am
   
Did you try anything? Where did you got stuck?
smrizvi1 at 12-May-12 8:51am
   
No i did not. I am not very familiar with Visual C++. I work in a library and have been given this task. My friend suggested me to copy all the records to a text file. He said with c++ we can search data in text file very easily. I am trying to find a solution on the internet, although there has been no luck.
smrizvi1 at 12-May-12 8:56am
   
Sir, do you think that this is the best format for turning these records to arrays and searching from them?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You could read the file into a hash_multimap[^] and then do a lookup based on the author name as key, but this will be limited by available memory. You may be better advised to use a database of some sort such that you can add, delete and update records as books are moved around. However, given your statement that you are not familiar with C++ you may be even better advised to pass this to an experienced programmer, or buy a ready made library management package.
  Permalink  
Comments
smrizvi1 at 12-May-12 9:18am
   
The problem is that I have got to do this myself. My boss asks for atleast the file handling code till coming monday. Where as i have to submit the whole task ,on visual c++, by the end of next week! I already have copied most of my records on a text file sir. And now i really want to do it that way! Only if you could help me with this please?
Richard MacCutchan at 12-May-12 11:29am
   
You need to talk to your boss and explain that you need more training and practice if he expects this to be done properly. Expecting this whole program to be completed within a week is really quite unrealistic, for someone who has very little experience. Forget about writing your own linked lists and make use of the STL containers that can do all the hard work for you. I already gave you a link to one of them so you should be able to find any others that may be useful.
 
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Alright, now this is what i have. I have used the approach of bubble sorting then binary searching. what i want to do now is to search a particular BOOK from someone without using the bubble sorting or binary searching method and instead search all the records one at a time, from start till the end. i think this will make the coding a lot shorter than it is now
 

//***************************************************************
//                   HEADER FILE USED IN PROJECT
//****************************************************************

#include<fstream> 
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
#include<iomanip>
#include<iostream>

 
using namespace std;
 
class BOOK
{
public:
	char ID[6];
	char Content[50];
	char AUTHOR[20];
	char Book[20];
  
	void create_book()// create new BOOK
	{
		cout<<"\nNEW BOOK ENTRY...\n";
		cout<<"\nEnter The BOOK ID.";
		gets(ID);
		cout<<"\n\nEnter BOOK NAME ";
		gets(Content);
		cout<<"\n\nEnter The AUTHOR's Name ";
		gets(AUTHOR);
		cout<<"\n\nEnter The Source Book ";
		gets(Book);
		cout<<"\n\n\nBOOK Created..";
		getch();
	}
 
	
	
	void report()
	{cout<<ID<<setw(10)<<Content<<setw(30)<<AUTHOR<<setw(20)<<Book<<endl;} //setw--> set width

	void Copy(BOOK hd){ //  this function copies whole record
		strcpy(ID,(hd.ID)); //copy BOOK ki id
		strcpy(AUTHOR,(hd.AUTHOR)); // copy BOOK ka AUTHOR
		strcpy(Book,(hd.Book));
		strcpy(Content,(hd.Content));
 
	}
 
};         //class ends here

void SwapBOOK(BOOK &hd1, BOOK &hd2){ //swaps position of BOOK1 with BOOK2

	BOOK Temp;            
 
	Temp.Copy(hd1);  //BOOK1 ko temp main copy kiya
	hd1.Copy(hd2);   //BOOK2 ko BOOK1 main copy kiya (now BOOK2 has moved to the place of BOOK1)
	hd2.Copy(Temp);  //BOOK1 ko finalyy BOOK2 ki jaga per copy ker diya
}
 

//***************************************************************
//    	global declaration for stream object, object
//****************************************************************
fstream fp; // fstream provides an interface to read and write data from files as input/output streams.
ofstream ofp; // ofstream provides an interface to write data to files as output streams
BOOK hd[1000]; //array of 1000 BOOK
int countBOOK=0; // initially there are zero aBOOK in the program

void ReportAll(){ // this function outputs a
	system("cls");
	for(int i=0;i<countbook;i++){>
		hd[i].report();
 
	}
	getche();
	
}
 

void writeAllBOOK()
{
	char ch;
	ofp.open("BOOK.dat",ios::trunc); // purani file (BOOK.dat) del ker k poora naya content overwrite ker do in new file
	//If the file opened for output operations already existed before, its previous content is deleted and replaced by the new one.
		system("cls");
		for(int i=0; i < countBOOK;i++){
 
			ofp.write((char*)&(hd[i]),sizeof(BOOK)); // hd[i] ko char samaj k write ker do BOOK
		
		}
 
		ofp.flush(); // abhi write ker do ... buffer istimal mat kero
    ofp.close();// close file
}
 
BOOK temp;
 
void LoadAllBOOK() 
{
	system("cls");
	fp.open("BOOK.dat",ios::in);// read file
	if(!fp) // if file doesnt opens
	{
		cout<<"ERROR!!! FILE COULD NOT BE OPEN ";
       		getch(); // character lo
			countBOOK =0;
       		return; // or waapis peechhay walay menu main chal do
     	}
 
	int i=0;
		while(fp.read((char*)&temp,sizeof(BOOK))) // file say  BOOK k siz ko as a string read ker raha hai and loading to  memory 
	{
		hd[i++].Copy( temp); // copying BOOK to main array
	}
		countBOOK=i; // jitni dafa ye kaam ho 'count BOOK' ko utni dafa increment day do
     	fp.close(); // close file
     	getch();
}
 
//***************************************************************
//    	SORT AND SEARCH BY AUTHOR (START)
//****************************************************************

 

void SortByAUTHOR() // bubble sort
{
	for(int i=0; i< countBOOK-1; i++){
		for(int j=0; j <countbook-1;>			if(strcmp(hd[j].AUTHOR,hd[j+1].AUTHOR)>0) 
				SwapBOOK(hd[j],hd[j+1]); // if above condition is satisfied, then call 'swapBOOK' function which is defined by us
		}
	}
 

}
 
//////////////////////////////////////////////////////////////////
void SearchBetweenIndex(int IndexA, int IndexB, char* AUTHOR)
{
	if(IndexA > IndexB){
		int temp;
		temp = IndexA;
		IndexA = IndexB;
		IndexB= temp;
	}
 
	for(int i = IndexA; i <= IndexB;i++){
		if(strcmp(hd[i].AUTHOR,AUTHOR)==0)
			hd[i].report();
	}
 
	getch();
}
 
/////////////////////////////////////////////////////////////////////

void ListByAUTHOR(char* AUTHOR){ //search by AUTHOR 	
	int PreviousIndex = 0; // first 

	int StartIndex =0 , EndIndex = countBOOK-1;
	
	int i=2;
	while(1==1){
		int CurrentIndex=(EndIndex+StartIndex)/2; //start searching from the mid position
		if(strcmp(hd[CurrentIndex].AUTHOR, AUTHOR) > 0){ 
			PreviousIndex = EndIndex;
			EndIndex = CurrentIndex;
		}else if(strcmp(hd[CurrentIndex].AUTHOR, AUTHOR) < 0){
			PreviousIndex = StartIndex;
			StartIndex = CurrentIndex;
		}else
			{
				SearchBetweenIndex(StartIndex, EndIndex, AUTHOR);
				break;
			}
 
		if(CurrentIndex == PreviousIndex)
			break;
	
		
 
	}
 
}
 

/////////////////////////////////////////////////////////////////////

void SortAndSearchByAUTHOR(){ // INPUT AUTHOR SEARCH CRITERIA
	system("cls");
	char str[50];
	cout << "Enter the Search Criteria for AUTHOR ";
	gets(str);
	SortByAUTHOR(); // CALL THIS FUNCTION
	ListByAUTHOR(str); // CALLL THIS FUNCTION

}
 
//***************************************************************
//    	SORT AND SEARCH BY AUTHOR ENDS
//****************************************************************

 
void main()
{
	char ch;
	LoadAllBOOK();
	do
	{
		system("cls");
		cout<<"\n\n\n\tMAIN MENU";
		cout<<"\n\n\t01. Create BOOK";
		cout<<"\n\n\t02. SORT And Search By AUTHOR";
		cout<<"\n\n\t03. lIST ALL ABOOK";
	  	cout<<"\n\n\t04. EXIT";
	  	cout<<"\n\n\tPlease Select Your Option (1-4) ";
	  	ch=getche();
	  	switch(ch)
	  	{
			case '1':system("cls");
				hd[countBOOK].create_book();
				countBOOK++;
			   	 break;
		  	case '2':
				SortAndSearchByAUTHOR();
			    	 break;
		    case '3':
				ReportAll();
				 break;
		  	case '4':
				writeAllBOOK();
				exit(0);
		  	default :cout<<"\a";
		}
    	}while(ch!='4');
}
 

</iostream></iomanip></string.h></process.h></stdio.h></conio.h></fstream>
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 420
1 OriginalGriff 210
2 Kornfeld Eliyahu Peter 210
3 DamithSL 210
4 mukesh mourya 85
0 OriginalGriff 7,215
1 DamithSL 5,199
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,757
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 18 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100