searching a text file for a word

This is a discussion on searching a text file for a word within the C Programming forums, part of the General Programming Boards category; i was given the assignment in college to write a program in c that will run from the command prompt ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    27

    searching a text file for a word

    i was given the assignment in college to write a program in c that will run from the command prompt and will search a desired text file for a desired word heres the code that i have and i think it should work but then im not sure so can any one help me with this

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
        int ch;
        char string[150];
        char *tokenptr;
        
        FILE *in_stream;
    
        if(argc==3)
        {
        if((in_stream=fopen(argv[1],"r"))==NULL)
        {
            fprintf(stderr,"Could Not Open File!");
            perror("Because");
            return 1; fclose(in_stream);
        }
        else
        {
        while(!feof(in_stream))
        {
            fgets(string,150,in_stream);
            printf("%s\n\n",string);
            tokenptr=strtok(string," ");
            while(tokenptr!=NULL)
            {
                ch=strcmp(tokenptr,argv[2]);
                if(ch==0)
                {
                    printf("%s was Found In File",argv[2]);
                }
                else
                {
                    printf("%s was not found in the file",argv[2]);
                }
    
            fclose(in_stream);
                return 0;
            }
            }
            }
        if(argc!=3)
            return 1;
        }
    }
    co the idea is that if my text file says "This is a text file and it can be searched"

    and at the dos window i type
    Code:
    C:\prog\debug\c_find mytextfile.txt can
    why does the reply always come back saying it cant find the word

    ps its called c_find as not to confuse my self with the dos command and im using vc6 if that helps

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    My guess from looking at your poor indentation is that you close the file after checking the first word.

    Also, there are a number of FAQ points which relate to your code.
    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.

  3. #3
    Registered User computerfreaks's Avatar
    Join Date
    Jan 2005
    Posts
    30
    I am unsure why your program doesn't work.... but I have recently being doing something similar and used the strstr function.... it seems to be a whole lot easier.

    the parameters are as follows:

    strstr(string1, string2)

    where string 1 is the word to search, and string 2 is the string to search within. The return of the function is the position of the searched string if it is found.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    27
    so i have been sitting down for the last couple of hours and ive just found my problem is the strcmp bit i was checking the thing wrong so the if statement after wards was looking for the wrong integer

    Code:
    ch=strcmp
    (tokenptr,argv[2]);
                if(ch==0)
    is should be
    Code:
    ch=strcmp
    (tokenptr,argv[2]);
                if(ch==1)
    thanks for any ideas

    EDIT: sorry for the mistake
    Last edited by satory; 02-22-2005 at 04:37 PM.

  5. #5
    Registered User computerfreaks's Avatar
    Join Date
    Jan 2005
    Posts
    30
    errrr those look the same to me?!?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    See the problem yet, when you have decent formatting?
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
        int ch;
        char string[150];
        char *tokenptr;
    
        FILE *in_stream;
    
        if (argc == 3) {
            if ((in_stream = fopen(argv[1], "r")) == NULL) {
                fprintf(stderr, "Could Not Open File!");
                perror("Because");
                return 1;
                fclose(in_stream);  /*!! HOW IS THIS REACHED? */
            } else {
                while (!feof(in_stream)) {
                    fgets(string, 150, in_stream);
                    printf("%s\n\n", string);
                    tokenptr = strtok(string, " ");
                    while (tokenptr != NULL) {
                        ch = strcmp(tokenptr, argv[2]);
                        if (ch == 0) {
                            printf("%s was Found In File", argv[2]);
                        } else {
                            printf("%s was not found in the file", argv[2]);
                        }
    
                        fclose(in_stream);  /*!! one word read, now bomb out */
                        return 0;
                    }
                }
            }
            if (argc != 3)  /*!! smooth - it's inside if ( argc == 3 ) */
                return 1;
        }
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 02:17 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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