Thread: seg fault with fgets!!!!

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    8

    seg fault with fgets!!!!

    Hey im getting a segmentation fault when running my program.

    I've narrowed it down to fgets in one of my functions.
    Im newish to programming and i also used gbd to find the segfault.

    The program does some of data manipulation, the function where the problem is reads two coloumns of data(doubles) from a file(180000 lines) into an array. the array is then used to do stuff, then populated with data from another similar file and so on....

    This is done about 1200 times, but always segfaults and iteration 1018.
    I had an earlier problem where it segfaulted at 500ish and was due to not fclose()'ing the file. But now the same problem but i cant seem to work out why its happening again!

    Im using a gcc complier. Below is the gdb results and code of function.

    Thanks alot for any help.
    Cheers.

    Code:
    Program received signal SIGSEGV, Segmentation fault.
    0x00007ffff78194dd in fgets () from /lib/libc.so.6
    (gdb) backtrace
    #0  0x00007ffff78194dd in fgets () from /lib/libc.so.6
    #1  0x0000000000415dff in populatedata ()
    #2  0x000000000041592e in integration ()
    #3  0x0000000000415839 in main ()
    The function code.

    Code:
    /** *ptr is pointer to beginning on outside declared array & *fn is pointer to the string with the file name where data is to be read **/
    
    int populatedata(double *ptr,char *fn)
    {
     double column1, column2;//Temp variables for readingline from data file
     char line[49]; //width of line parameter for fgets
     int i=0;
    
     FILE *fi;
             fi = fopen(fn, "r");//Open data file
             int before = 2; //for skipping the first 2 lines of data file
             int skip = 0;
    
     while (fgets(line,49,fi)!=NULL) //get each line from data file till EOF
            {
             if (skip < before)//skip first 2 lines
                      {
                       skip++;
                      } else {
     //read the 2 data values on line from file into  column vaiables.
                              sscanf(line, "%lf %lf", &column1, &column2);
                               *ptr=column2;
                               ptr++;
                               i++; //increment down the file
    
                            }
    
            }
    
     fclose(fi);
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Not the problem you are looking for, but a few other things:

    check that the file you open is not NULL before you try to access it
    variable i is not doing anything at all
    check the return value from sscanf to make sure it is reading the numbers you are after.
    column one is not doing anything, so why bother with it? If you don't care about the first value, just use %*f to ignore the first one in your sccanf format string.


    What is *ptr? At a glance, I would guess that you are writing off the end of whatever memory you allocated for it once you hit iteration 1018.
    Last edited by KBriggs; 06-23-2010 at 11:31 AM.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    8
    I had a puts(fn) and a printf(column1 & 2), in the code to confirm the right file and data being opened/read, but took it out before posting.

    *ptr is the pointer that is set to the start of the array where the data from the file is read into to.

    This is the code before the function.
    Code:
    double data[180100];
    double *ptr;
    ptr=&data[0];
    
        populatedata(ptr,fn);

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    You say you are only doing this 1200 times, why do you have 180100 slots in your array? Try making it waaaay smaller and see if it still segfaults.

    If you actually need it to be that big, use malloc.



    aside: this is how you should open files:

    Code:
    FILE *input;
    if ((input=fopen("inputfile","mode"))==NULL)
    {
         printf("Error opening file\n");
         ...handle error... //(usually just call abort() or return -1
    }
    and you certainly shouldn't ever remove this type of error checking, even if it happens to be working at the time.
    Last edited by KBriggs; 06-23-2010 at 04:16 PM.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    8
    The data files comes with 180000 lines and the array has to be filled with all of it each time.

    Have changed to malloc, will report on the results. Just takes time.......

    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a seg fault
    By ammochck21 in forum C Programming
    Replies: 11
    Last Post: 01-23-2009, 05:27 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM

Tags for this Thread