C++/sequential file
Expert: vijayan - 8/26/2009
QuestionQUESTION: Hi Vijayan
now i working on searching in sequential file with the key 'ID'.i've used binary_search function.
and want the user to insert an 'id' to find.the code doesn't have any errors but it has a crash during the runing.and as i worked on it i found that my problem is in binary_search function,but can't find whats wrong with it.could you plz check it out.i will be pleased if you help me?
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;
using std::cerr;
#include <vector>
#include <algorithm>
#include<string>
using std::string;
using std::getline;
#include<fstream>
using std::ofstream;
using std::ifstream;
#include<cstdlib>
using std::exit;
int main()
{
ofstream employee( "c:\\tests.dat",ios::app );
if(!employee)
{
cerr<<"File could not be opened" <<endl;
exit(1);
}
cout<<id<<endl;
cout<<"Enter the ID,Fname,Lname,City and Salary."<<endl
<<"Enter end-of-file to end input.\n?";
string ID;
char Fname[10];
char Lname[10];
char City[10];
double Salary;
while(cin>>ID>>Fname>>Lname>>City>>Salary)
{
employee<<ID<<' '<<Fname<<' '<<Lname<<' '<<City<<' '<<Salary<<endl;
cout<<"?";
}
std::vector< std::string > lines;//strores string values in 'lines'
// read each line into the vector
{
std::ifstream fin("c:\\tests.dat");
std::string line;
while( std::getline( fin, line ) ) lines.push_back( line );//get a 'line' from 'fin'
//function push_backavailable in all sequence containersto add an element to the end of the vector
}
string id;
cout<<"enter the ID to find: ";
getline(cin,id);
if (std::binary_search( lines.begin(), lines.end(), id ) )//<----------------------HERE IS THE PROBLEM!
cout << "\n\n was found in lines";
else
cout << "\n\n was not found in lines";
std::ofstream fout("c:\\tests.dat");
for( std::size_t i = 0; i < lines.size(); ++i )
cout << lines[i] << '\n';
cout<<"aaaaa";
return 0;
}
Thanx
Bita
ANSWER: There are several issues with this code.
1. The file is first opened for output in append mode
ofstream employee( "c:\\tests.dat",ios::app );
before it is closed, it is attempted to be opened for input
std::ifstream fin("c:\\tests.dat");
before it is closed, it is again attempted to be opened for output in truncate mode
std::ofstream fout("c:\\tests.dat");
This will not work correctly; close the file first if you want to open it again in a different mode.
cout<<"Enter the ID,Fname,Lname,City and Salary."<<endl
<<"Enter end-of-file to end input.\n?";
//...
while(cin>>ID>>Fname>>Lname>>City>>Salary)
{
//...
2. Once end-of-file is entered, std::cin will be in a fail and eof state. Nothing more can be read from std::cin while it is in this state.
3. std::binary_search can only be used on sorted sequences; the vector is not sorted.
---------- FOLLOW-UP ----------
QUESTION: thanx vijayan for answering
but about my second problem what should i do to input an id for searching,i tried a lot but it didn't work!
it seems that after inputting records in the file i can't get anything,is it right?
but i don't want to put the 'id' variable in the file,i want to get a value and put it in the 'id' variable and then search for it not in the file.
so why i can't use 'cin' or 'getline'?!
Thanx
Bita
AnswerThe problem is because after the input of records, std::cin is in an end-of-file state. Any attempt to read more items would fail.
You could rewrite the input loop as:
char more_input = 'y' ;
while( more_input != 'n' )
{
cin>>ID>>Fname>>Lname>>City>>Salary ;
employee<<ID<<' '<<Fname<<' '<<Lname<<' '<<City<<' '<<Salary<<endl;
cout << "add more records (y/n)?";
std::cin >> more_input ;
}
std::cin >> std::ws ; // eat up white spaces remaining in the input buffer.
std::string id ;
cout<<"enter the ID to find: ";
getline(cin,id);