Like Tree5Likes
  • 1 Post By jimblumberg
  • 3 Post By Salem
  • 1 Post By hk_mp5kpdw

'strcmp and strcpy' Not Working Correctly

This is a discussion on 'strcmp and strcpy' Not Working Correctly within the C Programming forums, part of the General Programming Boards category; I'm trying to make program that will read text from a file and show what words occur how many times. ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    11

    'strcmp and strcpy' Not Working Correctly

    I'm trying to make program that will read text from a file and show what words occur how many times.
    I'm trying to make it so that if a new string comes, it saves it into wlist[], and counts 1 to wcount[]. If the same word is there, it will just add 1 to wcount[]. The program will end when it gets to the end of a file.
    I don't know the reason, but I can't get strcmp and strcpy to work.
    I had to put a pointer in curword, just to make it compile.
    Help and advice is appreciated.
    Thank you in advance.
    Here is my source code so far:
    Code:
    #include <stdio.h>
    #include <string.h>
    #define LIST_MAX 100
    #define WORD_MAX 20
    
    int main(){
        int i, j, wcount
    [LIST_MAX];
        char str[15], wlist
    [LIST_MAX][WORD_MAX], *curword[20];
        FILE *textin;
    
        for(i=0; i<LIST_MAX; i++){
            for(j=0; j<WORD_MAX; j++){
                wcount[i] =0;
                wlist[i][j] =0;
                curword[i] =0;
            }
        }
    
        textin = fopen("assig2-text.txt", "r");
    
        while(fscanf(textin, "%s", str) !=EOF);{
            for(i=0;((i<LIST_MAX) && (curword[i]!=0)); i++){
                fscanf(textin, "%s", str);
                if((strcmp(curword[i], str)) == 0)
                    break;
            }
            if(wcount[i]>0){
                wcount[i]++;
            }
            else{
                strcpy(wlist[i], str);
                wcount[i]++;
            }
        }
        printf("*-- Word Status of 'assig2-text.txt' --*\n");
            for(i=0;(i<LIST_MAX) && (wlist[i]!=0); i++){
                printf("%s = %d \n", wlist[i], wcount[i]);
            }
        return 0;
    }
    Results are like this:
    *-- Word Status of 'assig2-text.txt' --*
    = 1
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0
    = 0

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,560
    Look closely at this loop with an empty body.
    Code:
    while(fscanf(textin, "%s", str) !=EOF);{

    Do you see that semicolon?

    Jim
    stahta01 likes this.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    11
    Oh, I've missed a simple typo.
    I've fixed it, but it still doesn't read correctly...
    I think the way I'm storing the strings are messed up.
    I've forgotten to put up the file I want to read:
    You can sit before the desk (or in front of the desk). The professor can sit on
    the desk (when he's being informal) or behind the desk, and then his feet are
    under the desk or beneath the desk. He can stand beside the desk (meaning next
    to the desk), before the desk, between the desk and you, or even on the desk (if
    he's really strange). If he's clumsy, he can bump into the desk or try to walk
    through the desk (and stuff would fall off the desk). Passing his hands over the
    desk or resting his elbows upon the desk, he often looks across the desk and
    speaks of the desk or concerning the desk as if there were nothing else like the
    desk. Because he thinks of nothing except the desk, sometimes you wonder about
    the desk, what's in the desk, what he paid for the desk, and if he could live
    without the desk. You can walk toward the desk, to the desk, around the desk, by
    the desk, and even past the desk while he sits at the desk or leans against the
    desk.
    This time, I got the program to say "You = 190". But the rest are all blank and 0's like before.
    I first want to ignore the periods, quote signs, and 'return's. Just want to get the separate strings down first.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,560
    Post your current code.

    Jim

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    Code:
     curword[i] =0;
            }
        }
     
        textin = fopen("assig2-text.txt", "r");
     
        while(fscanf(textin, "%s", str) !=EOF);{
            for(i=0;((i<LIST_MAX) && (curword[i]!=0)); i++){
                fscanf(textin, "%s", str);
                if((strcmp(curword[i], str)) == 0)
    Delete your code,
    Read this -> A development process
    And take it slower (don't try to write the whole thing at once).

    Start with something simple, like
    Code:
    while ( fscanf(textin, "%s", str) != EOF ) {
      printf("Read %s from file\n" );
      // next step, store in wlist
    }
    Build up a few lines at a time, and keep TESTING it to make sure it works.

    Writing the whole thing, then dumping it on a forum is not a long term strategy.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Code:
    
    #define LIST_MAX 100
    #define WORD_MAX 20
     
    int main(){
        int i, j, wcount[LIST_MAX];
        char str[15], wlist[LIST_MAX][WORD_MAX], *curword[20];
        FILE *textin;
     
        for(i=0; i<LIST_MAX; i++){
            for(j=0; j<WORD_MAX; j++){
                wcount[i] =0;
                wlist[i][j] =0;
                curword[i] =0;
            }
        }
    Your variable i goes from 0 - 99 in the outer for-loop. You set curword[i] to 0 but that variable is only valid up through a max index of 19. You are overstepping your array bounds because of this.
    Salem likes this.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    1,160
    Quote Originally Posted by hufnagel View Post
    I first want to ignore the periods, quote signs, and 'return's.
    You need to use those as delimeters to separate the words. Try using fgets() to read lines and strtok() to separate the lines into words.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp and strcpy error
    By behzad_shabani in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2008, 04:15 PM
  2. help with #define, strcmp, and strcpy
    By doc_watson2007 in forum C Programming
    Replies: 8
    Last Post: 12-20-2004, 09:50 PM
  3. strcpy()&strcmp()
    By mr alison in forum C Programming
    Replies: 5
    Last Post: 10-10-2002, 02:26 PM
  4. strcpy & strcmp, what is wrong?
    By Silverdream in forum C Programming
    Replies: 7
    Last Post: 02-13-2002, 02:36 PM

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