Thread: Segmentation Fault!!

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    6

    Segmentation Fault!!

    In creating a program to read an inventory list, ran into a segmentation fault, I tried removing lines in the program using // and still couldn't find the problem, Im pretty sure the problem is with the pointer not the NULL due to the testing iv done but not completely sure.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef struct
    {
        int item, volume, quantity;
        char description[50], author[30];
        int ISBN;
    }rectype;
    
    
    
    
    int main()
    {
        rectype rec;
        FILE *f;
    
    
    //initialize
        if ((f = fopen("GRANDVENTORY", "r")) == NULL)
        {
        printf("\n\n\n\n\n\t\t\tAWWWW FUUUUIII....Unable to open file GRANDVENTORY!!!\n\n\n\n\n");
        return(1);
        }
    do 
     {
       fscanf(f, "%d", rec.item);
         if (!feof(f))
         {
         fscanf(f, "%s", rec.description);
         fscanf(f, "%d", rec.volume);
         fscanf(f, "%d", rec.quantity);
         fscanf(f, "%s", rec.author);
         fscanf(f, "%d", rec.ISBN);
         printf("    %d \t\t %d \t\t %s \t\t %s \t\t %d \t\t %d \n", rec.item, rec.ISBN, rec.description, rec.author, rec.volume, rec.quantity);
         }
     }while(!feof(f));
    
    
     fclose(f);
    
    
    
    
     return(0);
    }
    Last edited by pbjorkman; 12-02-2011 at 04:28 AM. Reason: made progress

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    37
    just a guess:
    author is only 30 chars long, maybe you try to put there something with more chars?
    then you may fall in the printf.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    Just ran it and it didn't seem to change anything we still ran into the segmentation fault, the ISBN may be long for a %d but I dont think that would cause a segfault. Here is a sample from our Inventory

    10 9780135089729 (CSMART EBOOK) PROACTIVE POLICE MANAGEMENT THIBAULT 11 82 (item #, ISBN, Title, Author, Edition, Quantity)

    Thank you for looking into it, any further advice would be appreciated

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    When reading a value with %d format, an address needs to be passed. For example, fscanf(f, "%d", &rec.volume);

    If the ISBN is too long to be an int, scanf() will stop reading before reaching the end of the ISBN from the file. That may not cause a segmentation fault, but will certainly cause your file to be read incorrectly.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    You got it exactly, im embarrassed i missed that, thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault
    By lombardom in forum C Programming
    Replies: 8
    Last Post: 06-06-2010, 03:17 PM
  2. Segmentation fault
    By M-S-H in forum C++ Programming
    Replies: 15
    Last Post: 05-05-2010, 09:10 PM
  3. Segmentation fault
    By ksusha in forum C Programming
    Replies: 7
    Last Post: 04-27-2010, 10:04 AM
  4. Segmentation fault
    By tameeyore in forum C Programming
    Replies: 4
    Last Post: 02-26-2005, 01:49 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread