Thread: using strstr (Segmentation fault)

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    using strstr (Segmentation fault)

    Hi guys/gals,

    First post, read the faq etc... anyways... sorry for my lack of knowledge in C... I haave a "CSV" file
    that I am trying to read and locate text from... so far the reading part is not the problem.. according to valgrind all is well... untill I decide to use strstr() for my word allocation.... now this is were it keeps failing... I understand the error.. but I can not ping point how to fix it it... here is what I get from valgrind


    Code:
    ==5492== Invalid read of size 1
    ==5492==    at 0x4C25EC2: strlen (mc_replace_strmem.c:275)
    ==5492==    by 0x4E92EDF: puts (in /lib64/libc-2.9.so)
    ==5492==    by 0x4008EC: main (fieldFingers.c:49)
    ==5492==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
    and here is my code.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    
    struct fp_fields {
       char *f1;
    };
    
    int resetFile (FILE *fp)
    {
        int z;
    
        fseek (fp, 0, SEEK_END);
        z  = ftell (fp);
        fseek (fp, 0, SEEK_SET);
    
        return z; 
    }
    
    
    int main (void)
    {
        int fsize;
        char *f1, *buffer;
        char *fname = "users.CSV";
        FILE *fpntr;
        
        if ((fpntr = fopen (fname, "r"))  == NULL)
        {
           printf ("Error: %d\n", errno);
           return EXIT_FAILURE;      
        }
    
        fsize  = resetFile (fpntr);
        buffer = malloc (fsize * sizeof (int) + 1);
    
        while (fgets (buffer, 1024, fpntr) != NULL)
        {
    	 printf ("%s\n", strstr (buffer, "USERS"));
    	 
    	 //rfp_fields (buffer);
        }
      
        printf ("total bytes: %d\n", fsize);
    
        free (buffer);
        fclose (fpntr);
        
        buffer = '\0';
        return EXIT_SUCCESS;
    }
    anyways...I decided to use malloc on buffer cause I though that maybe that would help, but I guess not... sorry for my stupid code, its something that I am trying to do for work and trying to get in to C at the same time... as I did long ago... I also have a K&R book.. that is great finished reading it

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you just call rewind on your file?
    Why are you allocating a number * size of int for your buffer?
    Why are you using malloc at all?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > printf ("%s\n", strstr (buffer, "USERS"));
    Read the manual page for strstr()
    It can return NULL, which puts printf() up the creek without a paddle.
    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.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    2
    ahhh! right!! damn me... thanx Salem!!! your great.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Ques on String Manipulation
    By ckuttruff in forum C Programming
    Replies: 8
    Last Post: 06-22-2008, 09:32 PM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM
  5. strstr() segmentation fault help
    By Rpog in forum C Programming
    Replies: 7
    Last Post: 04-14-2004, 09:40 AM