Do not append if found in the binary file?

This is a discussion on Do not append if found in the binary file? within the C Programming forums, part of the General Programming Boards category; First, please view my code below. Code: #include <stdio.h> #include <stdlib.h> #include <string.h> struct noun { char singular[80]; char plural[80]; ...

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

    Do not append if found in the binary file?

    First, please view 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);
        int 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);
    
        }
    
        int addNouns(struct noun *p)
        {
            FILE *fp;
            int ret;
                
            fp = fopen("nouns.dat", "a");
    
            fread(&r, sizeof(struct noun), 1, fp); 
    
            while (!feof(fp))
            {
                if (p->singular == r.singular)
                {
                    printf("\nRepeat.");
                    exit(0);
                }
    
                else
                {
                    ret = fwrite(p, sizeof(struct noun), 1, fp); 
                    fclose(fp);
                    break;
                }
    
                fread(&r, sizeof(struct noun), 1, fp); 
    
            }
    
            return ret; 
        }
    
        void read()
        {
            FILE *fp;
    
            fp = fopen("nouns.dat", "r");
    
            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); 
    
                }
    
            }
    
        }
    I can read my binary files correctly but I can't identified the content of binary files correctly. For example, If user input "father", the binary files will have "father" and "fathers". Now is the main problem...if user key in "father" again. My program should tell user singular has repeat then I should not append this "father" into my binary files again. So far I has make some change in the addNouns function but I still can't detect if the record is repeat or non- repeat. So please help. Very thanks you.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your structs on line 12 and 13 are global - so you don't need to pass them as parameters to any functions.

    If you want to compare a current word, with words from a file, don't you want to save the words from the file in an array of the structs? I'd recommend it, but I'm not entirely sure I understand yet what you are trying to do.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    Well, I pass them as parameters because my assignment request the function must accept an input parameters so that why I pass to it. I try explain clearly then, This programs is to convert the singular input by user to plural. After the conversion, this programs will write the singular and plural data into a binary files. But if the binary files already contains the same data which the user input before, this programs should not write the same info into the binary files and will tell user. E.g. This singular has appended before, please input another singular.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you are required to pass a parameter to the function, then the parameter you are passing should be local (declare it at the top of main(), instead of above main().

    I'm not saying what you're doing won't work. I'm just saying, that's not the way it's done in C, and seems quite dodgy to me.

    You need to use a function like strcmp() on every word in your file, and see if any of them match the new word, don't you? I don't see any code to do that comparison.

    feof() is dodgy also. Much better to use the return value from the function that is reading the file data.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Append one file to another.
    By guillermoh in forum C Programming
    Replies: 6
    Last Post: 02-04-2008, 12:04 PM
  2. file append
    By rahulsk1947 in forum C Programming
    Replies: 2
    Last Post: 10-30-2007, 02:01 AM
  3. append binary file!
    By spank in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 01:39 PM
  4. Append to a file
    By Queatrix in forum Windows Programming
    Replies: 4
    Last Post: 07-12-2005, 03:12 AM
  5. append file?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 09-27-2001, 09:37 AM

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