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

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    8

    Angry keep getting segmentation fault! arrrggghhhh!!!!!!!!!!

    i have run the program multiple times, and have redone it four times. my buddy who's in the same class looked at it and said its similar to his, yet his works. this program is supposed to read file data and output it to a random access file.

    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define SIZE 500
    
    struct data{
            int id;
            char title [SIZE];
            char gene [SIZE];
            char express [SIZE];
            char chrom [SIZE];
            int scount;
    };
    
    int main (){
    
            int i;
            int j;
            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*/
            
            for (j=1; j<=SIZE; j++){
            fwrite(&genedata, sizeof(struct data), j, dataPtr);
            }
             
            fclose (dataPtr);
            }           /*close else*/
            
            return 0;
    }
    an example of the input data can be found here: http://texas.eng.uiowa.edu:8088/bme/59_006/Hs.small
    i greatly appreciate any help. thanks!
    steve

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    You have an if for if it doesn't open but your once your else ends you still try to write to the file descriptor thus a seg fault try
    Code:
    if(!dataPtr){
       perror("couln't open file\n");
       exit(1); /*stdlib is needed (which you have*/
    }
    also dont' use feof in a control loop see faq

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    8
    where whould i insert/replace that peice of code?

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> You have an if for if it doesn't open but your once your else ends you still try to write to the file descriptor <<

    No, that is just the poor formatting of the code.

    The seg fault is caused by the fact that the variable i is not initialised before use. Does no ones compiler warn about this?

    Code:
            for (j=1; j<=SIZE; j++){
            fwrite(&genedata, sizeof(struct data), j, dataPtr);
            }
    The results of this code are likely to be non-optimal.

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    8
    Quote Originally Posted by anonytmouse
    >> You have an if for if it doesn't open but your once your else ends you still try to write to the file descriptor <<

    No, that is just the poor formatting of the code.

    The seg fault is caused by the fact that the variable i is not initialised before use. Does no ones compiler warn about this?

    Code:
            for (j=1; j<=SIZE; j++){
            fwrite(&genedata, sizeof(struct data), j, dataPtr);
            }
    The results of this code are likely to be non-optimal.
    but i did intialize i and j (since you posted that code). im still cloudy on what you guys are saying.

  6. #6
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    in your code :
    Code:
    int i;
    ...
    genedata[i].id = atoi(line);
    just do:
    Code:
    int i = 0;

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Your code has lots of problems. Besides not initializing i, you didn't rewind your file before writing to it, you are writing 500 records when you only need to write the number of records which were read, and your fwrite() is writing too much data.

  8. #8
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    and another thing i noticed, you opened your file for reading with "r" mode, so you cannot write to it.

  9. #9
    Registered User
    Join Date
    Feb 2004
    Posts
    8
    Quote Originally Posted by viaxd
    and another thing i noticed, you opened your file for reading with "r" mode, so you cannot write to it.
    im not supposed to write to it, im supposed to create a random access file called records.dat

  10. #10
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    if you use c++ you can use "fstream.h"
    in these library you can open read and write in the same time in the file ...

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >im not supposed to write to it, im supposed to create a random access file called records.dat

    Is it possible to drop this class?
    Last edited by swoopy; 04-26-2004 at 03:00 PM.

  12. #12
    Registered User
    Join Date
    Feb 2004
    Posts
    8
    here's an updated version of my program:
    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;
    }

  13. #13
    Registered User
    Join Date
    Feb 2004
    Posts
    8
    Quote Originally Posted by swoopy
    >im not supposed to write to it, im supposed to create a random access file called records.dat

    Is it possible to drop this class?

    i wish, i wish...

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To be frank, and I'd rather not, because Frank isn't my name, your code is horribly flawed:
    Code:
    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)){
    1) If your file fails to open, you still end up reading from it anyway.
    2) Don't use feof, because it's not entirely accurate, and has its own problems. It's in the FAQ (link) that no one reads.
    3) I'll stop there, because until that's fixed, there's no point in continuing.

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

  15. #15
    Registered User
    Join Date
    Feb 2004
    Posts
    8
    ok, so how about this, Frank :P

    Code:
    if(( dataPtr = fopen("Hs.data.filtered", "r")) == NULL)
            {
                    printf("File could not be opened.\n");
                    return 0;
            }
    
    
            else{
    
            fscanf(dataPtr, "%s", temp);
            if(strcmp(temp, "//") != 0){
            fgets(line, SIZE, dataPtr);
            }
    
            /* while( !feof(dataPtr)){ */
            while (fgets(line, sizeof(line), dataPtr) != NULL){
    note: i still get the dreaded seg fault

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