// this is the grading sorting program for part one

#include <stdio.h>

int main(int argc, char **argv)
	FILE *fp; // file pointer, points to the file
	char file_name[32]; // store file name
	int ID[50];
	int grade[50];
	int a,b; // index variables
	int student_id, grades;
	// opening file by asking the user for the file name
	printf("Enter the name of the file containing the grades\n");
	fp=fopen("file_name", "r");
	// read in data into the arrays
	for (a = 0; a <= ID[50]; a++)
		fscanf(fp,"%d", &student_id);
		ID[a] = student_id;
		for(b = 0; b <= grade[50]; b++)
			fscanf(fp,"%d", &grades);
			grade[b] = grades;
		if(ID[a] == 0 && grade[b] == 0)
	return 0;

im trying to read data from a file by asking the user for the name of the file. when i input the name of the file and press enter it says: Segmentation fault PRESS enter. i don't what this problem is, im coding on ubuntu using codelite.

this is how the data looks like in the file

3050 76
2030 60
1600 70
2222 50
2430 60
2800 50
0 0

when the program reaches 0 0, it should stop reading, im not sure if this is the correct coding technique

the program is reading student ID's and student test score e.g 2800 50, first number is the i.d. second is the test score

im trying to store them into two seperate arrays then out put the i.d on a screen in ascending order

for (a = 0; a <= ID[50]; a++) There is no ID[50]. ID[] goes from 0-49 (50 entries). And you have no data in ID[50] to begin with for the comparison.

You want to:

Open the file
Set [B]a[/B] to 0
Start a [B]while[/B] loop 
         (you don't really know when the loop is supposed to end - 
          it depends on how many values in the file)
  Read the [B]ID[a][/B]
  Read the [B]GRADE[a][/B]
  add 1 to [B]a[/B]
end loop
Close the file

I'll let you figure out how to exit the loop.

I'm guessing 'a' should be your loop control variable? Then you probably meant for (a = 0; a < 50; a++)

i did this instead

#define SIZE 50
int ID

for (a = 0; a <= size; a++)

is this correct too

commented: Why? -4

No it isn't.

You are accessing an element of your array that does NOT exist. You are over-writing something in memory that exists past the end of your array. Any decent modern operating system will crash the program as soon as you reach the last element of the array.

To be *very* clear,

1. The size of your array is 50.
2. The last element you can safely access is 49.
3. for(a = 0; a < SIZE; a++)

you also got literal and variable mixed us at line 23. You better consider more than allocated amount of lines or other incorrect form so use return value of reading the file. You should also give maximum length for scanf.

You are repeating the same mistake on line 31 as well

Avoid using similar sounding names for variables .grade/ grades .... are very similar. Invariably you will use grade when you mean grades and the program will crash