1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

unknown error in c program

Discussion in 'C' started by Tensaijin, Dec 3, 2006.

  1. Tensaijin

    Tensaijin New Member

    Joined:
    Dec 3, 2006
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello,

    I have written a program in c which searches for a string in a text file. The program works, however I get a 'segmentation fault' error after the program terminates. I believe I have tracked the problem down to the while loop inside function Read(). Any help is much appreciated.

    Thanks,

    --------------------------------------------------------------------------
    Code:
    #include <stdio.h>
    #include <string.h>
    
    /* Declare static maximum array size */
    const int MAX_INPUT_SIZE = 60000;
    
    /************************************************** *************
    Task: Read text file and search for string
    Implementation: Asks the user to input a string they would like
    to search for, and loads each string array 'index' with a line
    from the file using strstr(). The program searches for the user's
    string as each line is read. The program then notifies the user
    of the results.
    ************************************************** **************/
    void Read(FILE* file)
    {
    char* _string;
    
    char token[MAX_INPUT_SIZE];
    
    int count = 0;
    
    printf("\nString to search for: ");
    
    fflush(stdout);
    
    fgets(token, 10, stdin);
    
    if (token[strlen(token) - 1] == '\n')
    {
    token[strlen(token) - 1] = '\0';
    
    } // End if
    
    while (fgets(_string, 1024, file) != NULL)
    {
    if (strstr(_string, token) != NULL)
    
    count++;
    
    _string = _string + 1;
    
    } /* End while */
    
    if (count != 0)
    
    printf("\nSuccess!!!\n\nString found: %d times\n\n", count);
    
    else
    
    printf("\nString not found\n\n");
    
    } /* End read */
    
    /************************************************** *************
    Task: Program entry point main()
    Implementation: main asks the user to input a file for reading.
    Afterwards it calls function Read() to search for a string.
    ************************************************** **************/
    int main(int argc, char *argv[])
    {
    FILE * doc;
    
    char filename[50];
    
    printf("\nFile to open: ");
    
    fgets(filename, 50000, stdin);
    
    /* Ensures the file name has no return characters */
    if (filename[strlen(filename) - 1] == '\n')
    {
    filename[strlen(filename) - 1] = '\0';
    
    } // End if
    
    /* Open file */
    doc = fopen(filename, "r");
    
    if (!doc)
    {
    printf("\nFile not found!\n");
    
    exit(1);
    
    } // End if
    
    /* With file opened, call function to read file */
    Read(doc);
    
    fclose(doc);
    
    return 0;
    
    } /* end main */
     
    Last edited by a moderator: Dec 4, 2006
  2. Aztec

    Aztec New Member

    Joined:
    May 9, 2006
    Messages:
    90
    Likes Received:
    0
    Trophy Points:
    0
    Allocate some memory for _string. Currently it's pointing to some unknown data.
     
  3. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    If you'd like to understand more about why you need to do that, see the link in my signature.
     
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
  5. Aztec

    Aztec New Member

    Joined:
    May 9, 2006
    Messages:
    90
    Likes Received:
    0
    Trophy Points:
    0
    I've read this before. Amazing tutorial Dawei.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    DaWei I have added your site to [thread=107]C-C++ books and tutorial[/thread]
     

Share This Page