Thread: Segmentation fault.

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 08:46 PM. Reason: code tags...

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, 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