Segmentation fault.

This is a discussion on Segmentation fault. within the C Programming forums, part of the General Programming Boards category; When i run this program i get the error segamentation fault (core dumped). I have checked my placeholders several times ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    7

    Segmentation fault.

    When i run this program i get the error segamentation fault (core dumped). I have checked my placeholders several times and can't seem to find the error.
    Can anyone tell me what causes segmentation fault. And if any of you have the time feel free to see if you can't find the error i've made.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #define max_number_of_matches 198
    
    
      struct match {
    	int round;
    	char day[3];
        char time[5];
    	char hometeam[3];
    	char awayteam[3];
    	int hometeam_score;
    	int awayteam_score;
      };
    	typedef struct match match;
    		
    	void read_resultater(const char *filename, match matches[]){
    		FILE *fp;
    		char day_m[3], time_m[5], hometm[3], awaytm[3];
    		int i, rnd, hometm_score, awaytm_score, scanres;
    		match local_match;
    		
    		fp=fopen(filename,"r");
    		
    		scanres = fscanf(fp, "%d  %s  %s  %s - %s  %d - %d", &rnd, day_m, time_m, hometm, awaytm, &hometm_score, &awaytm_score);
    		while (scanres ==7){
    		
    		 local_match.round = rnd;
    		 strcpy(local_match.day, day_m);
    		 strcpy(local_match.time, time_m);
    		 strcpy(local_match.hometeam, hometm);
    		 strcpy(local_match.awayteam, awaytm);
    		 local_match.hometeam_score = hometm_score;
    		 local_match.awayteam_score = awaytm_score;
    		 
    		 matches[i] = local_match;
    		 
    		 scanres = fscanf(fp, "%d  %s  %s  %s - %s  %d - %d", &rnd, day_m, time_m, hometm, awaytm, &hometm_score, &awaytm_score);
    		 i++;
    		}
    		
    		fclose(fp);	
    	}
    	
    	void print_match(match m){
    		printf("%d  %s  %s  %s - %s  %d - %d \n", m.round, m.day, m.time, m.hometeam, m.awayteam, m.hometeam_score, m.awayteam_score);
    	}
    	
    	int main(void){
    	 int i;
    		match matches_SAS_2009[max_number_of_matches];
    		read_resultater("resultater.txt", matches_SAS_2009);
    		
    		for(i=0; i <= 198; i++){
    		 print_match(matches_SAS_2009[i]);
    		}
    	return 0;
    }
    Last edited by Dizzy++; 12-12-2010 at 07:46 PM. Reason: code tags...

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    i <= 198
    No such thing as item #198 in your array. (You also should be using your max_whatever defined constant instead of a plain number here.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    7
    Shouldn't make any difference. and it doesn't i still get a segmentation error.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    1) If you want people to study your code, show the courtesy of wrapping it in the forum's code tags (click on the # icon in the top of the editor window, and paste your code, between the supplied
    [CODE ] [/CODE ] tags).

    2) Learn to narrow down your seg faults to at least one function, by trying it without calling one function at a time, and see how it does. Troubleshooting bugs in code is a necessary skill to develop, don't ignore it.

    And Welcome to the forum, Dizzy++!

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    7
    Okay so far i have tested my program bit by bit. And i believe that my error is in

    Code:
    void read_resultater(const char *filename, match matches[])
    And if I edit my while loop to be any other number than 7 it shows an unreadable portion of the txt file. What is the cause of this :S

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You write to matches[i] in your read function. Were you planning to give i a real value before you start?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    7
    Quote Originally Posted by tabstop View Post
    You write to matches[i] in your read function. Were you planning to give i a real value before you start?
    Well you are right aout that i should assign i a value. So i assigned it the value 0. After compiling, it still shows the txt file as unreadable, but still more readable than before. But still don't know what the error is

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What text file? Or do you mean that you are redirecting the output into a text file? You may very well get garbage at the end, since you are printing 199 results no matter how many get read.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    7
    So because i am reading to many lines of text it just gives me garbage?

  10. #10
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I don't see any initialization code for i in read_resultater() function. and you are using it to index array.
    As tabstop has pointed out, Array[N] valid indices 0 to N-1 inclusive.
    You are printing up to 199( 0-198) records. How the hell do you know that you've read that many records?

    Edit: are you sure that your input string are not longer than array sizes in your fscanf() call?
    Last edited by Bayint Naung; 12-12-2010 at 08:35 PM.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because you're reading too few lines. Your read function never bothers to tell anybody else how many lines get read, so you have no way of knowing, when you're printing, whether you're printing something valid or not.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,551
    Paste the first few lines of your input data file.

    > char day_m[3], time_m[5], hometm[3], awaytm[3];
    Because if any day or team name has more that TWO characters, then you're screwed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Most likely the OP is not taking into consideration the need to allocate 1 extra character for \0 at the end of his strings. So in his version 3 characters (i.e. FRA vs ENG) seem sufficient.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-20-2010, 10:55 PM
  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, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21