Like Tree23Likes

strange behaviour

This is a discussion on strange behaviour within the C Programming forums, part of the General Programming Boards category; Originally Posted by cfanatic Sounds like a useful function, but I am going to stick with what I know for ...

  1. #31
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,264
    Quote Originally Posted by cfanatic View Post
    Sounds like a useful function, but I am going to stick with what I know for now.
    Well I must admit, I had to look up the name of the function myself.

    However, there's nothing stopping you from writing a function that takes just a char and a string of chars and returns true if the char is in the string. It's then all your own code, and even easier to follow and work with.:
    Code:
    bool isOneOf(char ch, char *str)
    {
        while (*str != '\0')
        {
            if (ch == *str)
                return 1;
            ++str;
        }
        return 0;
    }
    
    //...
    
    if (isOneOf(ch, " .-!():;\n"))
    cfanatic likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  2. #32
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Quote Originally Posted by std10093;1125252
    [LIST
    In function spellcheck i do not get why this line
    Code:
     return word; // if no matches between word and any dictionary words, return word to main()
    is inside the loop.Maybe there is a reason for that.
    Are you saying that the line should not be in the function at all, or that it should be in another location in the function? I use the line so that any words in the textfile that does not match(because of wrong spelling) with the words in the dictionary file can be returned to main() for processing.

    Quote Originally Posted by oogabooga View Post
    spellcheck reads through the dictionary file. It's called multiple times, but I don't see the dictionary file being reset (or closed/reopened) anywhere.
    The reason why I didn't close the dictionary file was because I knew that it would need to be accessed lots of times, so left it open until the program was done with it. But if closing and reopening the file for every access is the right thing to do, then I will do it.

    Thanks for all the awesome replies. A lot to take in. I'll spend the rest of the day playing with the debugger.
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  3. #33
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Can't edit my last post.

    @ std10093: Just saw that AndiPersti has said that the "return word" statement is in the wrong location.
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  4. #34
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    cfanatic yes,the thing that oogabooga said has to be done (of course there are alternatives but this one is easy in my opinion ).By closing and reopening the dictionary image what happens...You use fgetc in your spellcheck function.So in order to check the first word from Hello world,you traverse through the whole dictionary,with no storing somewhere the index of the dictionary(no you shouldn't store it,no need,so bravo).So ok you checked Hello.Now what happens with the second word?How are you going to traversi through the the dictionary again?Now the pointer of the file has already reached EOF,so you have to reset it...Or done what oogabboga said (and i suggest too),close and open again file dictionary(which will reset the pointer and let you do the exact same thing again and again.
    Also,please notice that it is very elegant to open and close the file into the function spellcheck!Why?Because in this function you need this file,so why bothering the others who read your code with an extra file in main?Remember that functions are to enclose some jobs.

    As for the return word; ,yes this what i wanted to say,but Andreas put it better after me

    PS-indeed much posts,brave of you to keep track of all!BRAVO!

  5. #35
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Things are looking a bit better now. It still can't find a match for the first word, and the '.', which it replaces with '\0'. No I haven't checked with the debugger. But yes, I will use the debugger to check. That will take a few days to complete.

    test.txt = "hello world brown."

    dictionary
    Code:
    how 
    now
    hello 
    brown
    cow
    world
    purple
    reader
    output
    Code:
    hello: no match found
    world: matchfound
    brown: matchfound
    : no match found
    Code:
    // spellcheck.c - checks spelling of a text file against a dictionary file
    // 5 functions: copyfile(), getword(), spellcheck(), options(), editfile()
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define BUFFERSIZE 4096
    #define FILENAME_LENGTH 40
    #define STRING_LENGTH 300 // length of string read by fgets() from file
    #define WORD_LENGTH 40
    
    void copyfile (void);
    char *getword(FILE *tempfile);
    char *spellcheck (char *word);
    int  options(char *errorword);
    void editfile();
    
    int main(void)
    {
        FILE *tempfile;
        char *word, *errorword;
    
        copyfile();
        tempfile = fopen("temp.txt", "r");
        word = malloc(WORD_LENGTH * sizeof(char));
        errorword = malloc(WORD_LENGTH * sizeof(char));
        word = getword(tempfile);
    
        do
        {
            if(spellcheck(word) == NULL)
            {
                printf("%s: match found\n");
            }
            else
            {
                errorword = spellcheck(word);
                options(errorword);
                //editfile();
            }
        } while (getword(tempfile) != NULL);
    
        return 0;
    }
    
    // copy original file to tempfile for reading so that original file can be opened for writing(editing)
    void copyfile(void)
    {
        size_t c;
        char buffer[BUFFERSIZE];
        FILE *file, *tempfile;
    
        if((file = fopen("test.txt", "r")) == NULL || (tempfile = fopen("temp.txt", "w")) == NULL)
        {
            perror("Error");
        }
    
        while((c = fread(buffer, 1, BUFFERSIZE, file)) > 0)
        {
            fwrite(buffer, 1, c, tempfile);
        }
        fclose(file);
        fclose(tempfile);
    }
    
    char *getword(FILE *tempfile)
    {
    
        static char word[WORD_LENGTH]; // static to retain value of word
        int ch;
        int ctr = 0;
    
        while((ch = fgetc(tempfile)) != EOF)
        {
            if((ch != ' ') && (ch != '.') && (ch != '-') && (ch != '!') && (ch != '(') && (ch != ')') && (ch != ':') && (ch != ';') && (ch != '\n'))
            {
                word[ctr] = ch;
                ctr++;
            }
            else
            {
                word[ctr] = '\0'; // if ch is space or punctuation, add terminator to create string
                return word;
            }
        }
        return NULL; // flag that EOF has been reached
    }
    
    char *spellcheck(char *word)
    {
        int x, ch, flag, cmpr, ctr = 0;
        char buffer[BUFFERSIZE];
        char word_in_dict[WORD_LENGTH];
        FILE *dictionary;
    
        dictionary = fopen("dictionary.txt", "r");
    
        while((ch = fgetc(dictionary)) != EOF)
        {
            if((ch != '\n'))
            {
                word_in_dict[ctr] = ch;
                ctr++;
            }
            else
            {
                word_in_dict[ctr] = '\0'; // add terminator to make string
    
                if((cmpr = strcmp(word, word_in_dict)) == 0) // compare word to dictionary word
                {
                    return NULL; // match found, nothing further needs doing
                }
                ctr = 0; // reset array index to accept new word
            }
        }
        fclose(dictionary);
        return word; // if no matches between word and any dictionary words, return word to main()
    }
    
    int  options(char *errorword)
    {
        int x, ch;
    
    //    do
    //    {
            printf("%s: no match found\n", errorword);
    //        printf("Alternative words: \n");
    //        printf("0 to quit \n");
    //    } while(scanf("%d", &x) > 6);
    //
    //    if(x == 0)
    //    {
    //        exit(1);
    //    }
    }
    
    
    void editfile()
    {
    
    }
    Last edited by cfanatic; 10-01-2012 at 07:56 AM.
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  6. #36
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    cfanatic,your post needs to be re-edited

    Code:
     printf("%s: match found\n");
    You forgot to write the string
    Last edited by std10093; 10-01-2012 at 08:01 AM.
    cfanatic likes this.

  7. #37
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Done.

    Time for bed. Night.
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  8. #38
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    As i mentioned in another post,but ok i understand that many people posted so it is ok for you to miss it,the comparison fails because you are attaching the whitespaces in the word to compare.
    Right this it is going to be ok
    Code:
            if((ch != '\n'))
            {
                if(ch==' ')
                    continue;
                word_in_dict[ctr] = ch;
                ctr++;
            }
    EDIT - > goodnight
    cfanatic likes this.

  9. #39
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Quote Originally Posted by std10093 View Post
    As i mentioned in another post,but ok i understand that many people posted so it is ok for you to miss it,the comparison fails because you are attaching the whitespaces in the word to compare.
    Right this it is going to be ok
    Code:
            if((ch != '\n'))
            {
                if(ch==' ')
                    continue;
                word_in_dict[ctr] = ch;
                ctr++;
            }
    I didn't include the above code because there is no space in the dictionary file. Each word in the dictionary is on a new line.

    Here is the final code, and it seems to work fine without the above code.

    test.txt = "How now brown cow. A cowu has four brown legs."

    output
    Code:
    How: match found
    now: match found
    brown: match found
    cow: match found
    : no match found
    A: match found
    cowu: no match found
    has: match found
    four: match found
    brown: match found
    legs: match found
    : no match found
    Code:
    // spellcheck.c - checks spelling of a text file against a dictionary file
    // 5 functions: copyfile(), getword(), spellcheck(), options(), editfile()
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define BUFFERSIZE 4096
    #define FILENAME_LENGTH 40
    #define STRING_LENGTH 300 // length of string read by fgets() from file
    #define WORD_LENGTH 40
    
    void copyfile (void);
    char *getword(FILE *tempfile);
    char *spellcheck (char *word);
    int  options(char *errorword);
    void editfile();
    
    int main(void)
    {
        FILE *tempfile;
        char *word, *errorword;
    
        copyfile();
        tempfile = fopen("temp.txt", "r");
        word = malloc(WORD_LENGTH * sizeof(char));
        errorword = malloc(WORD_LENGTH * sizeof(char));
        word = getword(tempfile);
    
        do
        {
            if(spellcheck(word) == NULL)
            {
                printf("%s: match found\n", word);
            }
            else
            {
                errorword = spellcheck(word);
                options(errorword);
                //editfile();
            }
        } while (getword(tempfile) != NULL);
    
        return 0;
    }
    
    // copy original file to tempfile for reading so that original file can be opened for writing(editing)
    void copyfile(void)
    {
        size_t c;
        char buffer[BUFFERSIZE];
        FILE *file, *tempfile;
    
        if((file = fopen("test.txt", "r")) == NULL || (tempfile = fopen("temp.txt", "w")) == NULL)
        {
            perror("Error");
        }
    
        while((c = fread(buffer, 1, BUFFERSIZE, file)) > 0)
        {
            fwrite(buffer, 1, c, tempfile);
        }
        fclose(file);
        fclose(tempfile);
    }
    
    char *getword(FILE *tempfile)
    {
    
        static char word[WORD_LENGTH]; // static to retain value of word
        int ch;
        int ctr = 0;
    
        while((ch = fgetc(tempfile)) != EOF)
        {
            if((ch != ' ') && (ch != '.') && (ch != '-') && (ch != '!') && (ch != '(') && (ch != ')') && (ch != ':') && (ch != ';') && (ch != '\n'))
            {
                word[ctr] = ch;
                ctr++;
            }
            else
            {
                word[ctr] = '\0'; // if ch is space or punctuation, add terminator to create string
                return word;
            }
        }
        return NULL; // flag that EOF has been reached
    }
    
    char *spellcheck(char *word)
    {
        int x, ch, flag, cmpr, ctr = 0;
        char buffer[BUFFERSIZE];
        char word_in_dict[WORD_LENGTH];
        FILE *dictionary;
    
        dictionary = fopen("dictionary.txt", "r");
    
        while((ch = fgetc(dictionary)) != EOF)
        {
            if((ch != '\n'))
            {
                word_in_dict[ctr] = ch;
                ctr++;
            }
            else
            {
                word_in_dict[ctr] = '\0'; // add terminator to make string
    
                if((cmpr = strcmp(word, word_in_dict)) == 0) // compare word to dictionary word
                {
                    return NULL; // match found, nothing further needs doing
                }
                ctr = 0; // reset array index to accept new word
            }
        }
        fclose(dictionary);
        return word; // if no matches between word and any dictionary words, return word to main()
    }
    
    int  options(char *errorword)
    {
        int x, ch;
    
    //    do
    //    {
            printf("%s: no match found\n", errorword);
    //        printf("Alternative words: \n");
    //        printf("0 to quit \n");
    //    } while(scanf("%d", &x) > 6);
    //
    //    if(x == 0)
    //    {
    //        exit(1);
    //    }
    }
    
    void editfile()
    {
    
    }
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  10. #40
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    Maybe i did something in the copy paste operation and didn't match

    Now i suggest you to handle simple error for the files,like this that you can not find the file (it has not be created or something else is happening).See what fopen returns in this case and print a message and terminate

  11. #41
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Quote Originally Posted by std10093 View Post
    Maybe i did something in the copy paste operation and didn't match

    Now i suggest you to handle simple error for the files,like this that you can not find the file (it has not be created or something else is happening).See what fopen returns in this case and print a message and terminate
    What I want to do now is for words that are spelt incorrectly, is for the program to give the user a list of alternative words that they can choose to replace it with, and then the program replaces all instances of the word in the file. That is going to require probably 2 or 3 more functions. And maybe another week.
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  12. #42
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    Quote Originally Posted by cfanatic View Post
    What I want to do now is for words that are spelt incorrectly, is for the program to give the user a list of alternative words that they can choose to replace it with, and then the program replaces all instances of the word in the file. That is going to require probably 2 or 3 more functions. And maybe another week.
    Good idea.If i were you i would create a function like strcmp that counts how many equal characters the words to be compared have.I would this for every word of dictionary and then,the word from the dictionary that had the most equal characters would be the one to suggest to the user.Then let the user decide if he wants to replace or not.So you have to write a function for this too have fun
    cfanatic likes this.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange behaviour
    By cfanatic in forum C Programming
    Replies: 2
    Last Post: 07-16-2012, 06:41 AM
  2. strange behaviour
    By cfanatic in forum C Programming
    Replies: 9
    Last Post: 07-13-2012, 10:41 PM
  3. Strange behaviour of GCC
    By BlackOps in forum C Programming
    Replies: 14
    Last Post: 07-29-2009, 06:44 PM
  4. strange behaviour.......
    By surdy in forum C Programming
    Replies: 2
    Last Post: 05-01-2004, 11:50 AM
  5. Strange behaviour
    By PrivatePanic in forum Windows Programming
    Replies: 11
    Last Post: 07-23-2002, 12:54 AM

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