Like Tree2Likes
  • 1 Post By whiteflags
  • 1 Post By whiteflags

Compare string in binary files.

This is a discussion on Compare string in binary files. within the C Programming forums, part of the General Programming Boards category; Please take a look my code below. Code: #include <stdio.h> #include <stdlib.h> #include <string.h> struct noun { char singular[80]; char ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    67

    Compare string in binary files.

    Please take a look my code below.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
        struct noun
        {
            char singular[80];
            char plural[80];
    
        };
    
        struct noun p;
        struct noun r;
    
        void pluralize(struct noun *p);
        void addNouns(struct noun *p);
        void read();
    
        int main(void)
        {
            char choice;
    
            do {
    
                pluralize(&p);
                addNouns(&p);
    
                do {
    
                    flushall();
                    printf("More nouns? ");
                    scanf("%c", &choice);
                    choice = toupper(choice);
                    printf("\n");
    
                } while(choice != 'Y' && choice != 'N');
    
            } while (choice != 'N');
    
            printf("\n");
    
            read();
    
        }
    
        void pluralize(struct noun *p)
        {
    
            int x;
    
                printf("Please input singular: ");
                scanf("%s", &p->singular);
    
                // Store singular to plural
                strcpy(p->plural, p->singular);
    
                x = strlen(p->plural);
    
                // x-1 = Last character
                // x-2 = Before last character
                
                if(p->plural[x-1] == 'y') // y
                {
                    strcpy(&p->plural[x-1], "ies");
                }
    
                else if(p->plural[x-1] == 's') // s
                {
                    strcat(p->plural, "es");
                }
                
                else if(p->plural[x-2] == 'c' && p->plural[x-1] == 'h') // ch
                {
                    strcat(p->plural, "es");
                }
    
                else if(p->plural[x-2] == 's' && p->plural[x-1] == 'h') // sh
                {
                    strcat(p->plural, "es");
                }
    
                else if(p->plural[x-1] == 'x') // x
                {
                    strcat(p->plural, "es");
                }
    
                else
                {
                    strcat(p->plural, "s");
                }
    
                printf("\nPluralize Section");
                printf("\n=======================");
                printf("\nSingular: %s", p->singular);
                printf("\nPlural: %s\n\n", p->plural);
    
        }
    
        void addNouns(struct noun *p)
        {
            FILE *add, *read;
                
            read = fopen("nouns.dat", "rb");
    
            if(read == NULL)
            {
                add = fopen("nouns.dat", "a+b");
                fwrite(p, sizeof(struct noun), 1, add); 
    
            }
    
            else 
            {
                fread(&r, sizeof(struct noun), 1, read); 
    
                while (!feof(read))
                {
                    if (strcmp(r.singular, p->singular)==0)
                    {
                        printf("Record exist.");
                        exit(0);
                    }
    
                    else
                    {
                        add = fopen("nouns.dat", "a+b");
                        fwrite(p, sizeof(struct noun), 1, add); 
                        fclose(add);
                    }
    
                    fread(&r, sizeof(struct noun), 1, read); 
    
                }
    
            }
            
    
        }
    
        void read()
        {
            FILE *fp;
    
            fp = fopen("nouns.dat", "rb");
    
            if(fp == NULL)
            {
                printf("\nError.");
            }
    
            else 
            {
    
                fread(&p, sizeof(struct noun), 1, fp); 
    
                while (!feof(fp))
                {
    
                    printf("Singular: %s\n", p.singular);
                    printf("Plural: %s\n", p.plural);
    
                    fread(&p, sizeof(struct noun), 1, fp); 
    
                }
    
            }
    
        }
    My problem is the "record exist" message. My programs will compare the content in the binary with the user input. If user input a string which has been append into the binary before. It will tell user record exist then stop the program. But now the "record exist" message will continue display even user input a string which was not contain in the binary files so...

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    Your logic is to add the words to the file if they aren't there when you looked for them.
    Code:
    fread(&r, sizeof(struct noun), 1, read); 
    
    while (!feof(read))
    {
        if (strcmp(r.singular, p->singular)==0)
        {
            printf("Record exist.");
            exit(0);
        }
        else
        {
            add = fopen("nouns.dat", "a+b");
            fwrite(p, sizeof(struct noun), 1, add);
            fclose(add);
        }
    
        fread(&r, sizeof(struct noun), 1, read);
    }
    There are lots of structural problems here.

    This probably isn't what your file looks like, but it doesn't practically matter.

    apple
    banana
    orange

    say I look for the word grape. Since apple doesn't match the search word, grape is appended to the file by the else code.

    Now the file looks like

    apple
    banana
    orange
    grape

    Since banana doesn't match the search word, grape is appended to the file again. Rinse and repeat. Any word you search for is certainly there, either because it matched the first word, or because it was appended N times, and was eventually matched.
    Siaw Ys likes this.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    Ok...then what method should I use to solve this problem? Does change my variable into array form is recommend so that it will keep update?

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    My earlier post says that the problem is you don't exhaustively search the whole file to look for matches. As long as you do that first, there shouldn't be any problem with adding words after that. (And coincidentally, if the program is still running, that means you didn't find the word, so just add the word after the loop.)
    Siaw Ys likes this.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    Thanks you !!! I finally realize it ~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 06:56 PM
  2. String to Binary Files.
    By Charmy in forum C++ Programming
    Replies: 1
    Last Post: 07-01-2005, 06:49 PM
  3. Large Binary Compare
    By Robert Austin in forum C++ Programming
    Replies: 1
    Last Post: 02-07-2003, 07:52 AM
  4. I want binary compare
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 06-06-2002, 09:48 PM
  5. storing string objects to binary files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2001, 12:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21