C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Looking for a string Posted by dede on 25 Jan 2007 at 1:35 PM
How can I search for a specific word in a database file.
I used a structure as data structure.
it's just like that
struct vocab
{
char[30] word;
char[40] translation;
char[10] wordtype;
}

I can write and read data, but I would love to know how to find a word in this file depending on what a user types
thanks.
dede
Report
Re: Looking for a string Posted by stober on 25 Jan 2007 at 3:21 PM
: How can I search for a specific word in a database file.
: I used a structure as data structure.
: it's just like that
:
: struct vocab
: {
: char[30] word;
: char[40] translation;
: char[10] wordtype;
: }
: 

: I can write and read data, but I would love to know how to find a word in this file depending on what a user types
: thanks.
: dede
:


Your structure is incorrect -- but that is either just a posting typo, or if not then your compiler will complain bitterly to you :)

You will have to read the whole file one word at a time, for each word read compare it to whatever word the user types. Exactly how to do that depends on what language you are using -- C or C++.

Report
Re: Looking for a string Posted by dede on 25 Jan 2007 at 4:46 PM
I mixed up.
struct dictionary{
char word[50];
char translation[50];
char wordtype[10];
}voc;

but that's not the case here. so do I use fseek()? I just couldn't use it.
dede

Report
Re: Looking for a string Posted by stober on 27 Jan 2007 at 10:21 AM
This message was edited by stober at 2007-1-27 10:25:24

: I mixed up.
:
: struct dictionary{
: char word[50];
: char translation[50];
: char wordtype[10];
: }voc;
: 

: but that's not the case here. so do I use fseek()? I just couldn't use it.
: dede
:
:

why are you using fseek() ? There is no need for it. Just start at the beginning of the file and sequentually read it one word at a time until you find the word you want.

FILE *fp = fopen("filename.txt","r");
char word[50] = {0};
if(fp != NULL)
{
   while( fscanf(word,"%s",word) > 0)
   {
      // do something with this word

   }
}


or if you are writing c++ code
ifstream in("filename.txt");
std::string word;
while( in >> word )
{
   // do something with this word
}






Report
Re: Looking for a string Posted by Lundin on 30 Jan 2007 at 5:49 AM
: This message was edited by stober at 2007-1-27 10:25:24

: : I mixed up.
: :
: : struct dictionary{
: : char word[50];
: : char translation[50];
: : char wordtype[10];
: : }voc;
: : 

: : but that's not the case here. so do I use fseek()? I just couldn't use it.
: : dede
: :
: :
:
: why are you using fseek() ? There is no need for it. Just start at the beginning of the file and sequentually read it one word at a time until you find the word you want.
:
:
: FILE *fp = fopen("filename.txt","r");
: char word[50] = {0};
: if(fp != NULL)
: {
:    while( fscanf(word,"%s",word) > 0)
:    {
:       // do something with this word
: 
:    }
: }
: 

:
: or if you are writing c++ code
:
: ifstream in("filename.txt");
: std::string word;
: while( in >> word )
: {
:    // do something with this word
: }
: 

:
:
:


It will probably suffice for this case, but I'm pretty sure that's not how it is done in relational or OO SQL databases.

If I was some DB guru writing a DBMS I would implement it as a hash table and I would probably upload parts of the file(s) to RAM in some smart way before searching. Since hash tables won't get slower when the amount of data increases.
Report
Re: Looking for a string Posted by stober on 1 Feb 2007 at 4:56 AM
:
: It will probably suffice for this case, but I'm pretty sure that's not how it is done in relational or OO SQL databases.
:

I didn't think that is what the OP meant. There are probably as many ways to implement it in relational or SQL databases as there are software engineers to design them. I have no clue how they do it, never done it myself.
Report
Re: Looking for a string Posted by dede on 14 Feb 2007 at 9:08 AM
Well, I'm not planning to use RDB,, I just would love to have my own database,
Stober

I was trying to use fseek() because I thought it can return the value of am seeking, now I know it's not what I need. I think I need to throw the book I have in the stove :)
Thnaks guys
dede




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.