Thread: Segmentation Fault

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    28

    Segmentation Fault

    Can someone help me figure out why I am getting a seg fault at run time...

    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void){
    printf("Cake");/*debugging, didnt even reach this statement*/
    int data_in[10];
    FILE *pfile;
    pfile = fopen("input.txt", "r");
    int i,k,num;
    
         do{
    	fscanf(pfile,"%d",&num);
    	data_in[i] = num;
    	i++;
    	}while(pfile!=NULL);
    
    for(k=0; k<=10;k++){
    	printf("%d",data_in[k]);
    
    }
    
    return 0;
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Unless you are compiling in C99 mode, your mixed declarations are wrong. Beyond that, start checking return values of your functions, and keep an eye on making sure you don't run past the end of your buffer.

    The real problem is that i is used uninitialized.


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

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    28
    I have no other function in the program. I broke up all of my declarations and initialized all the variables to zero, but the seg fault is still there...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bigparker View Post
    I have no other function in the program. I broke up all of my declarations and initialized all the variables to zero, but the seg fault is still there...
    Quote Originally Posted by quzah View Post

    The real problem is that i is used uninitialized.

    Quzah.
    <echo> And did you make sure you didn't go over with k? (Hint: you went over with k.)

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    28
    Code:
    int main(){
    printf("Cake");
    int data_in[100];
    FILE *pfile;
    pfile = fopen("input.txt", "r");
    int i=0;
    int k=0;
    int num=0;
    
         do{
    	fscanf(pfile,"%d",&num);
    	data_in[i] = num;
    	i++;
    	}while(pfile!=NULL);
    
    for(k=0; k<=10;k++){
    	printf("%d",data_in[k]);
    
    }
    
    return 0;
    
    }

    after initializing "i" I still get the seg fault at run time...

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bigparker View Post
    Code:
    int main(){
    printf("Cake");
    int data_in[100];
    FILE *pfile;
    pfile = fopen("input.txt", "r");
    int i=0;
    int k=0;
    int num=0;
    
         do{
    	fscanf(pfile,"%d",&num);
    	data_in[i] = num;
    	i++;
    	}while(pfile!=NULL);
    
    for(k=0; k<=10;k++){
    	printf("%d",data_in[k]);
    
    }
    
    return 0;
    
    }

    after initializing "i" I still get the seg fault at run time...
    <echo> And did you make sure you didn't go over with k? (Hint: you went over with k.)

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    <echo> and keep an eye on making sure you don't run past the end of your buffer.



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

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    28
    Code:
    int main(){
    printf("Cake");
    int data_in[100];
    FILE *pfile;
    pfile = fopen("input.txt", "r");
    int i=0;
    
    int num=0;
    
         do{
    	fscanf(pfile,"%d",&num);
    	data_in[i] = num;
    	i++;
    	}while(pfile!=NULL);
    
    
    	printf("%d",data_in[0]);
    
    
    
    return 0;
    
    }

    I removed the for-loop and i just have print statement to print the value at data_in[0]...and the seg fault is still there. I cant seem to figure it out...

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    28
    Not to seem basic, but how do i test to be sure that i dont run past the end of my buffer?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could start by making sure you don't try to read in more than 100 numbers, since you're only allowing for 100 numbers in your array.


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

  11. #11
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Try following the value of pfile.

  12. #12
    Registered User
    Join Date
    Jun 2009
    Posts
    28
    thanks...that got rid of the seg fault. Do you guys have any idea of why the the program would only print the last value in my input file? its prints 421 repeatedly...

    Code:
    # define MAX 1000
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(){
    printf("Cake");
    int data_in[MAX];
    FILE *pfile;
    pfile = fopen("input.txt", "r");
    int i=0;
    int k=0;
    int num=0;
    
         do{
    	fscanf(pfile,"%d",&num);
    	data_in[i] = num;
    	i++;
    	}while((pfile!=NULL)&& (i<MAX));
    
    	for(k=0; k<MAX; k++){
    	printf("%d\n",data_in[k]);
    	}
    
    
    return 0;
    
    }

    my input file looks like this:

    Code:
    383
    886
    777
    915
    793
    335
    386
    492
    649
    421

  13. #13
    Registered User
    Join Date
    Jun 2009
    Posts
    28
    oh ok...I got that straight...it was because I had MAX set to a value greater than the number of integers in my input file...

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your program prints the ten numbers in the file, and then the last number an extra 990 times. You need to keep track of how many numbers get read from the file -- I think you think pfile is going to become NULL at end of file? It doesn't.

  15. #15
    Registered User
    Join Date
    Jun 2009
    Posts
    28
    oh ok i attempted to use this

    Code:
     while((j = fscanf(pfile, "%d", &num)) !=EOF){
    	data_in[i] = num;
    	i++;
    	}
    but it doesnt read from my input file i just get just values...what is the proper way to do this?
    Last edited by bigparker; 07-14-2009 at 06:55 PM. Reason: typo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 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

Tags for this Thread