Reading files from text and looking for text

This is a discussion on Reading files from text and looking for text within the C Programming forums, part of the General Programming Boards category; I have to write a program that reads in an ASCII text file and looks for a specified set of ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    61

    Reading files from text and looking for text

    I have to write a program that reads in an ASCII text file and looks for a specified set of words in it.
    The program should read in all of the words in text file and store them in an array of strings.

    Here is my main program below

    Code:
    #include <stdio.h>
    
    
    
    #define MAXCHAR    30
    #define MAXWORDS 30
    
    int check_word(char word_list[][MAXCHAR], int num_words, char word[]);
    
    void main(void)
    
    
    {
    
    
    
    
    FILE *infile = NULL;
        char infilename[MAXWORDS][MAXCHAR];
        char line[MAXCHAR];
        char words[MAXWORDS];
    
        int i,d,length,c,k=0;
    
    printf("Program to find specific words in a file\n");
    
        
        while(infile == NULL) {            /* input file name to read open */
            printf("Input filename:");
            scanf("%s",infilename);
            if((infile = fopen(infilename, "r")) == NULL) {
                printf("ERROR: file %s can not be opened!\n",infilename);
            }
        }
    
        printf("TEST 1\n");
    
    while(fgets(line,MAXCHAR,infile) != NULL) {
        length = strlen(line);
        if(line[length-1] == '\n') {            /* changes new line to null character */
            line[length-1] = '\0';
            length--;
        }
        printf("TEST 2\n");
        check_word(infilename[MAXWORDS][MAXCHAR],MAXWORDS,words[MAXWORDS]);
        printf("TEST 3\n");
        for(d=0;d<MAXWORDS;d++){
    
        printf("dictionary word %d \"%s\" occurs %d times",d,words[d],i);
    
    }
    }
    
    
    
    
    
        fclose(infile);
    
    
    }

    Here is my function below


    Code:
    #define MAXCHAR 30
    #define MAXWORDS 30
    #define NOT_FOUND -1
    
    
    
    int check_word(char word_list[][MAXCHAR], int num_words, char words[])
    
    
    
    {
    
    
        int i;
    
        for (i=0; i<num_words;i++)
            if(strncmp(word_list[i],words) == 0){
                return (words[i]);
            }
    
        return (NOT_FOUND);
    
    
    }
    It has to look for these words

    Code:
    for
    if
    else
    switch
    while
    case
    break
    main
    int
    float
    char
    double
    in this text file

    this file has a bunch of stuff in it. for example,
    it has a bunch of characters - main characters maybe. it
    also has a bunch of spaces. It doesn't have anything thing like int or
    {float} in it - case it did, that would % be double fun. !?!
    while this is good, we
    should ()switch off else it will be done for good...
    btw, this is not a char - char_buff
    Last edited by november1992; 04-26-2012 at 06:09 PM.

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    61
    My program crashes after line 45 (TEST 2). I think the problem is with my subprogram.

    Also, there is something wrong with my for loop near the end but I didn't fix it yet because the function doesn't get passed line 45.
    Last edited by november1992; 04-26-2012 at 06:11 PM.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You say it "crashes" but how can it even compile? strncmp takes 3 parameters, so clearly if you've actually run the program then you haven't posted the program you've run.
    Code:
    #define MAXCHAR 30
    #define MAXWORDS 30
    #define NOT_FOUND -1
    
    int check_word(char word_list[][MAXCHAR], int num_words, char words[])
    {
        int i;
    
        for (i=0; i<num_words;i++)
            if(strncmp(word_list[i],words) == 0){
                return (words[i]);
            }
        return (NOT_FOUND);
    }
    Also, you're not returning an int but a char*, so NOT_FOUND should be NULL, not -1.

    Your main is pretty screwed up too. Look through it carefully. Think. Improve your spacing. Get rid of the pointless blank lines. Think some more.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    61
    Well I was told that the function should return the number of times that the word is found in the text file, so shouldn't it be an int?
    And I just realized that the words I'm looking for aren't stored anywhere in my main. I just tried to put the words into an array but it says that there are "too many characters in constant".


    Code:
    #include <stdio.h>
    
    #define MAXCHAR    30
    #define MAXWORDS 30
    
    int check_word(char word_list[][MAXCHAR], int num_words, char word[]);
    
    void main(void)
        {
        FILE *infile = NULL;
        char infilename[MAXWORDS][MAXCHAR];
        char line[MAXCHAR];
        char words[] = {'for','if','else','switch','while','case',
            'break','main','int','float','char','double');
        int text[MAXWORDS];
    
        int i,d,length,c,k=0;
    
        printf("Program to find specific words in a file\n");
    
        while(infile == NULL) {            /* input file name to read open */
            printf("Input filename:");
            scanf("%s",infilename);
            if((infile = fopen(infilename, "r")) == NULL) {
                printf("ERROR: file %s can not be opened!\n",infilename);
            }
        }
        printf("TEST 1\n");
        while(fgets(line,MAXCHAR,infile) != NULL) {
            length = strlen(line);
            if(line[length-1] == '\n') {            /* changes new line to null character */
                line[length-1] = '\0';
                length--;
            }
            printf("TEST 2\n");
           
             for(d=0;d<MAXWORDS;d++){
                   text[d] = check_word(infilename[MAXWORDS][MAXCHAR],MAXWORDS,words[MAXCHAR]);
            }
            printf("TEST 3\n");
            
            for(d=0;d<MAXWORDS;d++){
    
                printf("dictionary word %d \"%s\" occurs %d times",d,words[d],text[i]);
            }
        }
        fclose(infile);
    }
    Code:
    #define MAXCHAR 30
    #define MAXWORDS 30
    #define NOT_FOUND -1
    
    int check_word(char word_list[][MAXCHAR], int num_words, char words[])
    {
        int i;
    
        for (i=0; i<num_words;i++)
            if(strncmp(word_list[i],words[i],num_words) == 0){
                return (i);
            }
    
        return (NOT_FOUND);
    
    
    }

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I was told that the function should return the number of times that the word is found in the text file, so shouldn't it be an int?
    In that case, yes. But it's not really counting the number of words, is it. It's just finding the first occurrence. To make it work you would have to (in main) actually READ the words from the file into the word_list array BEFORE every calling the check_word function. Then you'd have to call it for each word in your list of keywords. In check_word you need a separate counter variable, initialized to 0, that you increment every time you find the word. That's the variable you would return.

    Some points:

    Major errors

    infilename should be declared as char infilename[MAXCHAR] (or whatever size you want).

    words should be declared as char *words[].

    The words for words should be in double-quotes, not single.


    Minor (but still important) errors

    You should include <string.h> since you're using functions from it.

    main should be declared as int main(void) and should return 0 as its last statement.


    Other than that, and although you've definitely started thinking a little more, you've really got to tighten down that thinking cap and give it another go. The way you're using infilename is ... insane. Try again.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    61
    Hmm, I just found out that I have to read in the words from the text file and then store them into a two dimensional array. I'm sure I'm not doing it right because now my program will not even run. Ok I realized what I did wrong and I'm trying to fix it.

    EDIT: So I found out how to read the words from the text file and then store them into a 2D array.

    So now it will crash when it reaches the "TEST 2" Marker. I'm not sure how to make the return value for the check_word() function cycle through the different values in the array when I print it out.

    EDIT: I put the values into an array of ints. But I still don't know why it's crashing.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    
    #define MAXCHAR    30
    #define MAXWORDS 30
    #define TEXTWORDS 12
    
    int check_word(char word_list[][MAXCHAR], int num_words, char word[]);
    
    int main(void)
        {
        FILE *infile = NULL;
        FILE *infile2 = NULL;
        char infilename[MAXCHAR];
        char line[MAXCHAR];
        int text[MAXWORDS];
        char *words[MAXWORDS][MAXCHAR];
    
        int i,d,length,a,c,k=0;
    
        printf("Program to find specific words in a file\n");
    
      
        while(infile == NULL) {            /* input file name to read open */
            printf("Input filename:");
            scanf("%s",infilename);
            if((infile = fopen(infilename, "r")) == NULL) {
                printf("ERROR: file %s can not be opened!\n",infilename);
            }
        }
    
        infile2 = fopen("dictionary.txt","r");
        for(c=0;c<MAXWORDS;c++){
            for(a=0;a<MAXCHAR;a++){
                fscanf(infile2,"%s",words[c][a]);
            }
        }
        printf("TEST 1\n");
        while(fgets(line,MAXCHAR,infile) != NULL) {
            length = strlen(line);
            if(line[length-1] == '\n') {            /* changes new line to null character */
                line[length-1] = '\0';
                length--;
            }
            printf("TEST 2\n");
            for(d=0;d<TEXTWORDS;d++){
             text[d] = check_word(words[MAXWORDS][MAXCHAR],TEXTWORDS,infilename[MAXCHAR]);
            }
            
            printf("TEST 3\n");
            
            for(d=0;d<TEXTWORDS;d++){
                printf("dictionary word %d \"%s\" occurs %d times",d,words[d],text[d]);
            }
        }
        fclose(infile);
        
        return(0);
    }

    Code:
    #define MAXCHAR 30
    #define MAXWORDS 30
    #define NOT_FOUND -1
    
    int check_word(char word_list[][MAXCHAR], int num_words, char words[])
    {
        int i,k;
    
        for (i=0,k=0; i<num_words,k<num_words;i++,k++)
            if(strncmp(word_list[i],words[i],num_words) == 0){
                return (k);
            }
    
        return (NOT_FOUND);
    
    }
    Last edited by november1992; 04-26-2012 at 08:43 PM.

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    61
    I just realized that these lines of code don't do what I want them to do.
    Code:
    printf("TEST 1\n");
        while(fgets(line,MAXCHAR,infile) != NULL) {
         length = strlen(line);
         if(line[length-1] == '\n') {            /* changes new line to null character */
                 line[length-1] = '\0';
                 length--;
            }
    They were supposed to remove newline characters from the text and replace them with '/0' characters. But it still crashes so there still is another problem. I changed it to this:

    Code:
    printf("TEST 1\n");
        while(fgets(infilename,MAXCHAR,infile) != NULL) {
          length = strlen(infilename);
          if(infilename[length-1] == '\n') {            /* changes new line to null character */
                 infilename[length-1] = '\0';
                 length--;
            }

    EDIT:I'm not even sure if this code is necessary.
    Last edited by november1992; 04-26-2012 at 09:21 PM.

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    You are not checking to see if dictionary.txt is open before using it.

    First, check to see if the array is being filled properly - Try printing all the words.

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    61
    It crashes when I try to print it out.

    This is what it looks like when I run the file without printing the array. It repeats about 10 more times.

    http://i45.tinypic.com/1o16jp.png

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Corrected the first problem - try and run this, as it is now.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    
    #define MAXCHAR    30
    #define MAXWORDS 30
    #define TEXTWORDS 12
    
    int check_word(char word_list[][MAXCHAR], int num_words, char word[]);
    
    int main(void)
        {
        FILE *infile = NULL;
        FILE *infile2 = NULL;
        char infilename[MAXCHAR];
        char line[MAXCHAR];
        int text[MAXWORDS];
        char *words[MAXWORDS][MAXCHAR];
    
        int i,d,length,a,c,k=0;
    
        printf("Program to find specific words in a file\n");
    
         while(infile == NULL) {            //  input file name to read open 
            printf("Input filename:");
            scanf("%s",infilename);
            if((infile = fopen(infilename, "r")) == NULL) {
                printf("ERROR: file %s can not be opened!\n",infilename);
            }
        }
    
      
    /*
     This is the first error I see. You can't store a word, into a space
    that is the size of one char. This version should work OK.
    */
    
      infile2 = fopen("dictionary.txt","r");
       //corrected, and changed to use 'i' for the index variable 
       for(i=0;i<MAXWORDS;i++) {  
            fscanf(infile2,"%s",words[i]);
        }
    
       /* test it */
       printf("Let's test it:\n");
       for(i=0;i<MAXWORDS;i++)
           printf("%s\n",words[i]);
    
      
       printf("How'd we do? \n"); getchar(); getchar();
    
    
        /* test it and see how it does, to this point */
    /*
        printf("TEST 1\n");
        while(fgets(line,MAXCHAR,infile) != NULL) {
            length = strlen(line);
            if(line[length-1] == '\n') {            // changes new line to null character 
                line[length-1] = '\0';
                length--;
            }
            printf("TEST 2\n");
            for(d=0;d<TEXTWORDS;d++){
             text[d] = check_word(words[MAXWORDS][MAXCHAR],TEXTWORDS,infilename[MAXCHAR]);
            }
            
            printf("TEST 3\n");
            
            for(d=0;d<TEXTWORDS;d++){
                printf("dictionary word %d \"%s\" occurs %d times",d,words[d],text[d]);
            }
        }
    */
        fclose(infile);
        
        return(0);
    }
    Last edited by Adak; 04-27-2012 at 04:51 PM.

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    61
    It prints it out along with some random things. http://i45.tinypic.com/1553x2q.png
    Also, you stored it into a 1D array. The instructions for my assignment say it has to be stored into a 2D array

    "The list of words to be searched for is contained in a second file, named dictionary.txt. The dictionary.txt file contains one word per line. The program should read in all of the words in the dictionary.txt file and store them in an array of strings (i.e., a 2D array of characters). You can assume that the dictionary.txt file has 30 or fewer words in it and each word has fewer than 30 characters. An example dictionary.txt file is shown below (provided as part of the assignment):"

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by november1992 View Post
    It prints it out along with some random things. http://i45.tinypic.com/1553x2q.png
    Also, you stored it into a 1D array. The instructions for my assignment say it has to be stored into a 2D array
    Not at all. You have an extra * before the array name:
    Code:
    char *words[MAXWORDS][MAXCHAR];
    Which makes words a 3 dimension array. Remove the * from before the words array name.

    When you see
    Code:
    fscanf(yourFilePointer, "%s ", words[i]);
    That doesn't mean that I'm storing the word into a 1D array. That means that, in C, the base of the array is a constant pointer to the first element of the array. Also, in C, arrays of 2D, are always arrays of 1D, inside an array.

    So words[i] IS being used as a pointer (which fscanf() requires), to the first element of the 1D array for that particular word.

    As i changes (increments in this case), the "row" of the word being stored, will change.

    Try it again, but without the * this time.

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,666
    Quote Originally Posted by Adak View Post
    Also, in C, arrays of 2D, are always arrays of 1D, inside an array.
    I'm not sure what to make of that statement. It is true that C stores two dimensional arrays in a contiguous manner (in one piece) such that a calculation like *(p + i * X + j) can be made for p[i][j], if the array is declared p[X][Y]. It's also true that 2-d arrays are arrays of arrays. I just don't think that's what you said. If you put the array on the free store though, 2-d arrays do not need to be contiguous, see Question 6.16.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In a REAL 2D array, the 1D arrays will always be inside the 2D array. What you're describing is a collection of pointers to 1D arrays. They are similar to a real 2D array, but are they? No.

  15. #15
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,666
    Actually I tried to cover all of the bases, and talked about 2-d arrays on the stack. I don't think arrays are ever "inside" one another. I guess I'm just not being clear today.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading text files
    By Xinco in forum C++ Programming
    Replies: 20
    Last Post: 01-09-2007, 12:24 PM
  2. Reading text files?
    By Kate in forum C Programming
    Replies: 3
    Last Post: 06-30-2006, 01:22 AM
  3. reading text files
    By stimpyzu in forum C++ Programming
    Replies: 11
    Last Post: 04-17-2004, 07:45 AM
  4. Reading Text files
    By AmazingRando in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2003, 08:30 PM
  5. Reading text files
    By JaWiB in forum C++ Programming
    Replies: 8
    Last Post: 02-16-2003, 10:58 PM

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