Word Search

This is a discussion on Word Search within the C Programming forums, part of the General Programming Boards category; I am getting a run time error (This program has performed an illegal action and will be shut down) I ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    99

    Word Search

    I am getting a run time error (This program has performed an illegal action and will be shut down) I have narrowed it down to the approximate area where fread is called to get the contents of a file and begin the search for a word.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 200
    
    static int cmp( char *a, char *b, int len);
    static char *search( char *text, char *word, int len);
    
    int main(int argc, char *argv[])
    {
    int i,x=1;
    char *buf,*found;
    FILE *fd;
    
    if (argc<2)
    {
         printf("Usage: WORD <file> <word>\n");
         return 1;
    }
    
    strcat(argv[1], ".txt");
    if ((fd=fopen(argv[1], "r+")) == NULL)
    {
         printf("File execution failure\n");
         return 1;
    }
    
    while ( (fread(buf, sizeof(char),MAX ,fd )) != 0)
    {
    	while ( (found = search(buf, argv[2], strlen(argv[2]))) != NULL)
    		printf("%d)     %s",x++,*found);
    }
    
    printf("\n\nDone\n");
    fclose(fd);
    return 0;
    }
    
    static int cmp( char *a, char *b, int len)
    {
    	for (; *a == *b; a++, b++)
    	{
    		if (--len==0)
    			return 0;
    	}
    	return 1;
    }
    
    static char *search( char *text, char *word, int len)
    {
    	for (; *text != '\0'; text++)
    	{
    		if ( cmp(word,text,len) == 0)
    			return text;
    	}
    	return NULL;
    }

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    > if (argc<2)

    This should be:
    Code:
    if(argc < 3)
    {
         printf("Usage: %s <file> <word>\n", argv[0]);
         return 1;
    }
    argv[0] = <progam name>
    argv[1] = <file>
    argv[2] = <word>
    argc = 3

    > strcat(argv[1], ".txt");

    I'm not sure this is a good idea. Maybe you should copy argv[1] to another buffer:
    Code:
    char buf[BUFSIZ];
    sprintf(buf, "%s.txt", argv[1]);
    Another thing: if you use fread, your buffer will not be null terminated!!! Use fgets or add the null character yourself.

  3. #3
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>fread(buf, sizeof(char),MAX ,fd )
    You kinda forgot to tell buf to point to something valid, reading to a pointer to garbage memory is an access violation. Program go boom :-)
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Firefox and Google Search
    By DeepFyre in forum Tech Board
    Replies: 0
    Last Post: 01-16-2005, 09:28 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. finding strings in strings
    By watshamacalit in forum C Programming
    Replies: 14
    Last Post: 01-11-2003, 12:08 AM

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