Feb 29, 2012 at 5:25am UTC
I have created dat file in that bookno and bookname, i want to search word giving by dynamically from the user and search that word in bookname in dat file and the bookno and bookname display on the screen
any one help me please
Last edited on Feb 29, 2012 at 5:27am UTC
Feb 29, 2012 at 5:33am UTC
Hi.
If you're using C++, I think the std::string::find function might be of some use, if you don't know it already.
http://cplusplus.com/reference/string/string/find/
That's all the help I can really give at the moment, since you haven't posted any code. :/
-Albatross
Last edited on Feb 29, 2012 at 5:33am UTC
Feb 29, 2012 at 5:53am UTC
Hi,
i display my code below please help me.
void search()
{
int i;
char bookno;
char bookname[20],word[40];
int count = 0;
cout<<"enter name to search";
cin>>word;
fin.open("book.dat", ios_base::in);
// bool blineBreak = false;
// int nFileIndex = 0, nFileOffset = 0;
while(!fin.eof())
{
// fin >> word;
// nFileOffset = nFileIndex;
// cout<< word;
// blineBreak = false;
//int lineno = 131;
//int count = 0;
//string str;
//while(count < 130 && getline(fin, word))
//count++;
//getline(fin, word);
getline(fin,bookname);
cout<<bookname<<endl;
/*sscanf(word,"%d$%s",&bookno,&bookname);
if(strcmp(bookname,word)==0)
{
*/
// strcpy(word,"\0");
cout<<bookno<<endl;
// cout<<bookname<<endl;
// }
}
fin.close();
}
this is my code for searching a word in dat file in c++ . How can i search dynamic input to the dat file and print bookno and bookname on the screen
any one help me please
Last edited on Feb 29, 2012 at 6:22am UTC
Feb 29, 2012 at 6:36am UTC
I have two suggestions.
1. You may want to change those char[]s into std::strings. You'll need to #include <string>, but it should make your task a lot easier, especially since you will be able to use the find function.
2. Please put your code in [co
de]
/*Code here!*/
[/co
de] tags. That way, it'll make it much easier for us to help you. :)
EDIT: I took the code from the post below and put it in code tags here.
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
void search()
{
int i;
char bookno;
char bookname[20],word[40];
int count = 0;
cout<<"enter name to search" ;
cin>>word;
fin.open("book.dat" , ios_base::in);
// bool blineBreak = false;
// int nFileIndex = 0, nFileOffset = 0;
while (!fin.eof())
{
// fin >> word;
// nFileOffset = nFileIndex;
// cout<< word;
// blineBreak = false;
//int lineno = 131;
//int count = 0;
//string str;
//while(count < 130 && getline(fin, word))
//count++;
//getline(fin, word);
getline(fin,bookname);
cout<<bookname<<endl;
/*sscanf(word,"%d$%s",&bookno,&bookname);
if(strcmp(bookname,word)==0)
{
*/
// strcpy(word,"\0");
cout<<bookno<<endl;
// cout<<bookname<<endl;
// }
}
fin.close();
}
-Albatross
Last edited on Feb 29, 2012 at 7:01am UTC
Feb 29, 2012 at 6:43am UTC
/*code here*/
void search()
{
int i;
char bookno;
char bookname[20],word[40];
int count = 0;
cout<<"enter name to search";
cin>>word;
fin.open("book.dat", ios_base::in);
// bool blineBreak = false;
// int nFileIndex = 0, nFileOffset = 0;
while(!fin.eof())
{
// fin >> word;
// nFileOffset = nFileIndex;
// cout<< word;
// blineBreak = false;
//int lineno = 131;
//int count = 0;
//string str;
//while(count < 130 && getline(fin, word))
//count++;
//getline(fin, word);
getline(fin,bookname);
cout<<bookname<<endl;
/*sscanf(word,"%d$%s",&bookno,&bookname);
if(strcmp(bookname,word)==0)
{
*/
// strcpy(word,"\0");
cout<<bookno<<endl;
// cout<<bookname<<endl;
// }
}
fin.close();
}
i have try to find from dat file but im unable to find and display on the screen .If u have alter my code mean alter it and send me
please help me
Feb 29, 2012 at 7:11am UTC
Hi
Can i have sample code for searching string in dat file and display that relevant data from the dat file.
for me its very urgent please help me
Last edited on Feb 29, 2012 at 7:13am UTC
Feb 29, 2012 at 1:46pm UTC
Hi,
how can i search surname in dat file in c++
for example in dat file i save like below mentioned
1$maths
2$science
3$socialscience
if i search by sci name mean for me it have to display like below
2 science
3 socialscience
from the above both the field contain sci so it display like the above
any one help me please how to do program for this i need sample source code for this .
Feb 29, 2012 at 3:03pm UTC
Use
std::string
along with
std::getline()
and
std::string::find()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <string>
#include <iostream>
#include <fstream>
int main()
{
std::ifstream file( "file_name" ) ;
std::string search_str = "whatever" ;
std::string line ;
int line_number = 0 ;
while ( std::getline( file, line ) )
{
++line_number ;
if ( line.find(search_str) != std::string::npos )
std::cout << "line " << line_number << ": " << line << '\n' ;
}
}
Last edited on Feb 29, 2012 at 3:06pm UTC
Mar 1, 2012 at 5:54am UTC
Hi Mr/Mrs/Miss JLBorges ,
Thanks for Your suggestion, my program has been working successfully. Can you explain each line of your sample code that you have send to me.
Please explain and help me
Last edited on Mar 5, 2012 at 8:53am UTC