Thread: Segmenation Fault

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    10

    Segmenation Fault

    Hi, I'm trying to run a program that contains the following code. When running it, the program crashes; however, when debugging it, I get segmentation Fault error. Can someone please help. thanks.
    Code:
    void fileheader(char fn[],int *a )
    
    {
    
            int c;
            FILE *fp;
            fp=fopen(fn,"r");
            while((c=getc(fp)) != '\n');
                    while ((c=getc(fp)) == '#')
                      {while ((c=getc(fp)) != '\n');}
            ungetc(c,fp);
            fscanf(fp,"%d%d%d",&a[0],&a[1],&a[2]);
            fclose(fp);
    }

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Sure. Tell us what platform and IDE you are using, and we'll tell you how to use the debugger.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Code:
    void fileheader(char fn[],int *a )
    
    {
    
            int c;
            FILE *fp;
            fp=fopen(fn,"r");// is fn null-terminated?
            while((c=getc(fp)) != '\n'); //moves filepointer pass leading newlines
            while ((c=getc(fp)) == '#')//while 1st char in row is '#' ---indentation was misleading
            {
    		while ((c=getc(fp)) != '\n');// move to next row
    	} 
            ungetc(c,fp);//unget last read??, why?? .. probably points at newline now
            fscanf(fp,"%d%d%d",&a[0],&a[1],&a[2]);// is "a" defined as an array of at least 3 int?  Are you reading past end of file?
            fclose(fp);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  2. C Segmenation fault
    By OrbiT^ in forum C Programming
    Replies: 3
    Last Post: 04-20-2005, 10:58 AM
  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. segmenation fault when executing
    By v3dant in forum C Programming
    Replies: 2
    Last Post: 11-15-2004, 04:07 PM