Thread: Reading from a logfile.

  1. #1
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50

    Angry Reading from a logfile.

    I'm sorry, but I have no idea what's going wrong. Could you guys please help me out? I'm trying to make a program that will read a logfile, and output any lines which contain the subString "fault".

    I keep getting a segmentation fault.

    Here is my code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void readLogFile( FILE*);
    
    char* readLine( FILE*);
    
    void outputThings( char*);
    
    int main( int argc, char** argv)
    {
            FILE* logfile;
            int worked;
    
            if ( argc != 2)
            {
                    printf("\n\nFORMAT: %s <logname>\n\n", argv[0]);
            } else
            {
                    logfile = fopen( argv[1], "r");
                    if( logfile == NULL)
                    {
                            perror("error in main function of main.c");
                            worked = 0;
                    } else
                    {
                            readLogFile( logfile);
                            worked = fclose( logfile);
                    }
            }
    
            return worked;
    }
    
    void readLogFile( FILE* logfile)
    {
            char* line;
            char* failStr;
    
            while( !feof( logfile))
            {
                    /*printf("DEBUG feof\n"); WORKED */
    
                    line = readLine( logfile);
    
                    failStr = strstr( line, "fail");
    
                    printf("%s", failStr);
                    /*This is for debug. It keeps printing (null) and then gets a seg fault. Without it I get a Seg fault too. :( 
                    */
                    if ( failStr != NULL)
                    {
                            printf("DEBUG failString\n");
                            outputThings( line);
                    } /* else just skip it. */
    
    
            }
    }
    char* readLine( FILE* logfile)
    {
            char* line = (char*)malloc( 300*sizeof( char) );
            char ch;
    
            while( ch != '\n')
            {
            /*      printf("DEBUG while ch != 'newLineChar')\n"); WORKED */
                    ch = fgetc( logfile);
                    line += ch;
            }
    
            return line;
    }
    
    
    void outputThings( char* line)
    {
            int day, hour, min, sec;
    
            char* month = (char*)malloc(3*sizeof( char));
            char* process = (char*)malloc(30*sizeof( char));
            char* message = (char*)malloc(200*sizeof( char));
    
            sscanf( line, "%s %d %d:%d:%d %s: %s", month, &day, &hour, &min, &sec, process, message);
    
            printf("%s %d %d:%d:%d %s: %s\n", month, day, hour, min, sec, process, message);
    
    }

    As you can see, I've used printf() to see what's working and not working.

    Thanks in advance,
    The Doctor
    Last edited by The Doctor; 09-09-2012 at 10:53 PM. Reason: I didn't copy the code over correctly.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    "line" is a un-initialised pointer and will only exist in that scope.

    line += ch will add to line the ascii value of whatever the ch returns.
    Last edited by Click_here; 09-09-2012 at 10:18 PM.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Reinterpretation of Click_here's post:

    "line" does not point to any storage space so the character read is loaded somewhere unknown -- probably causing your segfault.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50
    Oh.. okay... Also, I just realized that I hadn't copied the code over properly. It looked as though 'ch' hadn't been initialized at all in one of the functions.

    I will try to fix it according to your suggestions.

    EDIT: Wait, where was line uninitialized? I think that must have been the incorrectly copy/pasted code I put up.

  5. #5
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50
    Okay, so I've changed it to a for-loop-like while loop. And this is my output (It's missing the message at the end I think, but I'll worry about that later.)

    Aug 19 4:0:1 automount[1478]::
    Aug 19 5:0:1 automount[1478]::
    Aug 19 6:0:1 automount[1478]::
    Aug 19 7:0:1 automount[1478]::
    Aug 19 8:0:1 automount[1478]::
    Aug 19 9:0:1 automount[1478]::
    Aug 19 10:0:1 automount[1478]::
    Aug 19 11:0:1 automount[1478]::
    Aug 19 12:0:1 automount[1478]::
    Aug 19 13:0:1 automount[1478]::
    Aug 19 14:0:1 automount[1478]::
    Aug 19 15:0:1 automount[1478]::
    Aug 19 16:0:1 automount[1478]::
    Aug 19 17:0:1 automount[1478]::
    Aug 19 18:0:1 automount[1478]::
    Aug 19 19:0:1 automount[1478]::
    Aug 19 20:0:1 automount[1478]::
    Aug 19 21:0:1 automount[1478]::
    Aug 19 22:0:1 automount[1478]::
    Aug 19 23:0:1 automount[1478]::
    Aug 20 0:0:2 automount[1478]::
    Aug 20 1:0:1 automount[1478]::
    Aug 20 2:0:1 automount[1478]::
    Aug 20 3:0:1 automount[1478]::
    Aug 20 4:0:1 automount[1478]::
    Aug 20 5:0:1 automount[1478]::
    Aug 20 6:0:1 automount[1478]::
    Aug 20 7:0:1 automount[1478]::
    Aug 20 8:0:1 automount[1478]::
    Aug 20 9:0:1 automount[1478]::
    Aug 20 10:0:1 automount[1478]::
    Aug 20 10:15:35 automount[1478]::
    *** glibc detected *** ./logreader: free(): invalid next size (normal): 0x0000000000a23530 ***
    ======= Backtrace: =========

    *SNIP*

    It then proceeds to show paths, and a memory map. Then says "Aborted (core dumped)".

    Here's the change to the code above:
    Code:
    char* readLine( FILE* logfile)
    {
            char* line = (char*)malloc( 300*sizeof( char) );
            char ch = ' ';
            int ii=0;
    
            while( ch != '\n')
            {
            /*      printf("DEBUG while ch != 'newLineChar')\n"); WORKED */
                    ch = fgetc( logfile);
                    line[ii] = ch;
                    ii++;
            }
    
            return line;
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Maybe the amount of space allocated is not enough. You should be checking that, and either stop reading when the array is fully used, or expand the dynamic array's capacity.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50
    Can you link me to a good place that tells me what you mean by expanding the dynamic array's capacity?

    Do you mean just creating a bigger array, and moving everything over to that?

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    if the size of the input data is bigger than your array, use the realloc function.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What are your requirements for this program? Your first post indicated you could use simple logic like:

    Code:
    open the file
    create a char array, comfortably larger than the largest line of text in the log file: log[]
    
    while(there is another line in the file, use fgets to store it) {
       use strstr() to check if the stored line has the target string in it
       if it does, 
          handle this
       else
          handle that
       
       handle any other calculations needed
    
       create the output required
    }//end of while
    close the file
    Which would eliminate all this mucking about with malloc() left, right, and center, as well as making the program simpler to understand, debug, and shorten it's run-time.

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Your program crashes after reading the last line.

    readLine() reads the last line in your file including the newline character. But that doesn't mean that feof() returns 1 because you haven't reached EOF yet.
    Thus the while loop starts another iteration and the first character you get in readLine is EOF (ch will be -1). But in readLine() you never check if ch is EOF and you infinitely try to read from the file until the array index (ii) is far out of bounds and the OS reacts with a seg fault.

    You could easily try that for yourself in the debugger of your choice. Just use a short single line text file and step through the program.

    BTW: You allocate new memory for each new line but never free any of if. So I agree with Adak's suggestion to use a big enough buffer array instead.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help converting CANoe logfile to EXCEL readable .txt file
    By turbofizzl in forum C Programming
    Replies: 21
    Last Post: 08-16-2009, 08:56 PM
  2. reading into an int
    By Cpro in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2008, 01:30 PM
  3. Reading from txt
    By cppdungeon in forum C++ Programming
    Replies: 5
    Last Post: 09-25-2006, 05:12 PM
  4. reading from a mic
    By realcr in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-22-2006, 11:52 AM
  5. reading a bmp
    By da_dupa in forum C Programming
    Replies: 1
    Last Post: 06-24-2004, 12:57 PM