Thread: unexplained behaviour

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242

    unexplained behaviour

    Hi all. In my searchstring() function, why does the last while() loop execute 11 times? It doesn't matter if I set TEXTLENGTH to 100,000 or 9 million, it always executes 11 times. The text file is less than 10K characters, so shouldn't the fgets return value be NULL because it has passed the end of file indicator? I want to search for the first occurrence of a string in the text file.

    Thanks in advance.

    Code:
    // searchfilesforsting.c: search for strings in text file and replace them with another string
    // 3 functions: openfile(), searchstring(), replacestring()
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define FILENAMELENGTH 40
    #define STRINGLENGTH 1024
    #define TEXTLENGTH 10000
    
    char string[STRINGLENGTH], filename[FILENAMELENGTH], *text;
    FILE *file;
    
    void openfile(void);
    void searchstring(void); // search for string in file
    void replacestring(void);
    
    int main(void)
    {
        openfile();
        searchstring();
    
    
        return 0;
    }
    
    void openfile(void)
    {
        do
        {
           printf("Enter file to open: ");
           fgets(filename, FILENAMELENGTH, stdin);
           filename[strlen(filename) - 1] = '\0'; // known filenames. no overflow problems
        } while((file = fopen(filename, "r"))== NULL);
    }
    
    void searchstring(void)
    {
        if((text = calloc(TEXTLENGTH, sizeof(char))) == NULL) // allocate memory for reading text
        {
            puts("Memory allocation error");
            exit(EXIT_FAILURE);
        }
    
        do
        {
            printf("Enter string to search for: ");
        } while(fgets(string, STRINGLENGTH, stdin) == NULL);
    
        string[strlen(string) - 1] = '\0'; // known strings. no overflow problems
    
        while((fgets(text, TEXTLENGTH, file)) != NULL)
        {
            if(strstr(text, string) != NULL)
            {
                puts("A match has been found");
            }
    
            else
            {
                puts("A match has NOT been found");
            }
        }
    
        free(text);
        fclose(file);
    }
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by cfanatic View Post
    Hi all. In my searchstring() function, why does the last while() loop execute 11 times? It doesn't matter if I set TEXTLENGTH to 100,000 or 9 million, it always executes 11 times. The text file is less than 10K characters, so shouldn't the fgets return value be NULL because it has passed the end of file indicator?
    How many lines has your file? 11?
    fgets() stops reading after a newline or EOF (whatever comes first).

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by cfanatic View Post
    why does the last while() loop execute 11 times? It doesn't matter if I set TEXTLENGTH to 100,000 or 9 million..
    You miss understand the meaning of fgets() second parameter. It's telling the size of the buffer. As Andreas said fgets will try to read a line of text. If it finds '\n' before the string length is buffersize - 1 then the returned string includes the '\n' plus the '\0' string terminator otherwise it stops reading but also appends '\0'.

    I want to search for the first occurrence of a string in the text file.
    I f you want to read the whole file into a buffer use fread().
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unexplained crashes with GCC...
    By mikemhz in forum C Programming
    Replies: 2
    Last Post: 11-05-2011, 11:22 AM
  2. Sleep() causing unexplained reduction in cpu load
    By sajanphilip in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2008, 02:58 AM
  3. Unexplained Crashing
    By Swordsman in forum C Programming
    Replies: 9
    Last Post: 05-10-2007, 05:24 AM
  4. Unexplained "unhandled exception"
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 04-19-2007, 11:19 AM
  5. Unexplained Segfault
    By crepincdotcom in forum C Programming
    Replies: 6
    Last Post: 08-24-2006, 04:19 PM