Looking for a string

How can I search for a specific word in a database file.
I used a structure as data structure.
it's just like that
[code]
struct vocab
{
char[30] word;
char[40] translation;
char[10] wordtype;
}
[/code]
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:-)

Comments

  • : How can I search for a specific word in a database file.
    : I used a structure as data structure.
    : it's just like that
    : [code]
    : struct vocab
    : {
    : char[30] word;
    : char[40] translation;
    : char[10] wordtype;
    : }
    : [/code]
    : 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++.

  • I mixed up.
    [code]
    struct dictionary{
    char word[50];
    char translation[50];
    char wordtype[10];
    }voc;
    [/code]
    but that's not the case here. so do I use fseek()? I just couldn't use it.
    dede:-)

  • [b][red]This message was edited by stober at 2007-1-27 10:25:24[/red][/b][hr]
    : I mixed up.
    : [code]
    : struct dictionary{
    : char word[50];
    : char translation[50];
    : char wordtype[10];
    : }voc;
    : [/code]
    : 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.

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

    }
    }
    [/code]

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





  • : [b][red]This message was edited by stober at 2007-1-27 10:25:24[/red][/b][hr]
    : : I mixed up.
    : : [code]
    : : struct dictionary{
    : : char word[50];
    : : char translation[50];
    : : char wordtype[10];
    : : }voc;
    : : [/code]
    : : 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.
    :
    : [code]
    : FILE *fp = fopen("filename.txt","r");
    : char word[50] = {0};
    : if(fp != NULL)
    : {
    : while( fscanf(word,"%s",word) > 0)
    : {
    : // do something with this word
    :
    : }
    : }
    : [/code]
    :
    : or if you are writing c++ code
    : [code]
    : ifstream in("filename.txt");
    : std::string word;
    : while( in >> word )
    : {
    : // do something with this word
    : }
    : [/code]
    :
    :
    :


    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.
  • :
    : 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.
  • Well, I'm not planning to use RDB,, I just would love to have my own database,
    [code]Stober[/code]
    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:-)

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories