Thread: keep getting segmentation fault! arrrggghhhh!!!!!!!!!!

  1. #16
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> 1) If your file fails to open, you still end up reading from it anyway. <<

    This thread is like that puzzle, count the fs in the paragraph, that goes around periodically. Due to the spacing, our brains absolutely refuse to accept that the closing brace under fgets is matching the opening brace at the end of the if rather than closing the else.

    So, sdogg45, here is your code with some semblence of decent formatting. (The problems are still there). Try to use it or something similar as example formatting and it will make your code much easier to read (and write).

    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define SIZE 50
        
    struct data
    {
        int id;
        char title [SIZE];
        char gene [SIZE];
        char express [SIZE];
        char chrom [SIZE];
        int scount;
    };
        
    int main ()
    {
        int i = 0;
        int j = 0;
        struct data genedata[SIZE];
        FILE * dataPtr;
        char temp [SIZE];
        char line [SIZE];
        
        if(( dataPtr = fopen("Hs.data.filtered", "r")) == NULL)
        {
            printf("File could not be opened.\n");
        }
        else
        {  
            fscanf(dataPtr, "%s", temp);
    
            if(strcmp(temp, "//") != 0)
            {
                fgets(line, SIZE, dataPtr);
            }
      
            while(!feof(dataPtr))
            {
                if (strcmp(temp, "ID") == 0)
                {
                    genedata[i].id = atoi(line);
                }
        
                if (strcmp(temp, "TITLE") == 0)
                {
                    strcpy(genedata[i].title, line);
                }
    
                if (strcmp(temp, "GENE") == 0)
                {
                    strcpy(genedata[i].gene, line);
                }
         
                if (strcmp(temp, "EXPRESS") == 0)
                {
                    strcpy(genedata[i].express, line);
                }
        
                if (strcmp(temp, "CHROMOSOME") == 0)
                {
                    strcpy(genedata[i].chrom, line);
                }
         
                if (strcmp(temp, "SCOUNT") == 0)
                {
                    genedata[i].scount = atoi(line);
                }
        
                i++;
         
                fscanf (dataPtr, "%s", temp);
        
                if (strcmp(temp, "//") != 0)
                {
                    fgets (line, SIZE, dataPtr);
                }
            } /*close while*/
         
            if(( dataPtr = fopen( "records.dat", "w")) == NULL)
            {
                printf("error");
            }
            else
            {
                for (j = 1;j <= i;j++)
                {
                    fwrite(&genedata, sizeof(struct data), j, dataPtr);
                }
            }
        
            fclose (dataPtr);
        }   /*close else*/
        
        return 0;
    }
    Last edited by anonytmouse; 04-26-2004 at 04:09 PM.

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This thread is like that puzzle, count the fs in the paragraph, that goes around periodically. Due to the spacing, our brains absolutely refuse to accept that the closing brace under fgets is matching the opening brace at the end of the if rather than closing the else.
    Didn't even see it. That's what happens when you don't stick with a consistant formatting in your code. Some braces at the end of lines, some on their own line...

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

  3. #18
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Dont' worry Quzah I fell for it too. And on a one line if statement at that

  4. #19
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    I tried your code out....I couldnt really tell what exactly was causing the segfault, but it works for me after some slight adjustments...

    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define SIZE 50
        
    struct data
    {
        int id;
        char title [SIZE];
        char gene [SIZE];
        char express [SIZE];
        char chrom [SIZE];
        int scount;
    };
        
    int main ()
    {
        int i, j;
        struct data genedata[SIZE];
    
        FILE * dataPtr;
        char line [SIZE];
            
        if(( dataPtr = fopen("hstest.txt", "r")) == NULL)
        {
            printf("File could not be opened.\n");
        }
        else
        {
            i=-1;
            while(fgets(line, SIZE, dataPtr) && i<SIZE)
            {
                char *p = strchr(line,'\n');
                if (p != NULL)
                    *p='\0';
                    
                if (strncmp(line,"//",2) == 0)
                    i++;
                   
                if (strncmp(line, "ID",2) == 0)
                    genedata[i].id = atoi(line+3);
        
                else if (strncmp(line, "TITLE",5) == 0)
                    strcpy(genedata[i].title, line);
    
                else if (strncmp(line, "GENE",4) == 0)
                    strcpy(genedata[i].gene, line);
         
                else if (strncmp(line, "EXPRESS",7) == 0)
                    strcpy(genedata[i].express, line);
        
                else if (strncmp(line, "CHROMOSOME",10) == 0)
                    strcpy(genedata[i].chrom, line);
         
                else if (strncmp(line, "SCOUNT",6) == 0)
                    genedata[i].scount = atoi(line+7);
                
            } /*close while*/
            fclose (dataPtr);
            
            i++;
                 
            if(( dataPtr = fopen( "records.dat", "w")) == NULL)
                printf("error");
            else {
                for (j = 0;j < i;j++)
                    fprintf(dataPtr,"[id:%d]\t%s\t%s\t%s\t%s %d\n\n",
                            genedata[j].id,genedata[j].title,genedata[j].gene,
                            genedata[j].express,genedata[j].chrom,genedata[j].scount);
        
                fclose (dataPtr);
            }
        }   /*close else*/
        
        return 0;
    }
    Last edited by nonpuz; 04-26-2004 at 06:42 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM