hello all
anybody could please tell me how to change this code to make it read from a file that has unknown no of students in it. now this part of the programe read only 5 students.

could you please help me to solve this problem as soon as you can.
thanks alot. :sad: :sad:

Code:

#include <stdio.h>

#include <string.h>
#include <stdlib.h>


struct student {
	char name[25];
	int marks[5];
};

void main()
{ 
	FILE *fp;
	struct student s[5];

	int i,j,u;
	double ave;

	fp=fopen("c:\\test.txt","r");
	if(fp==0)
	{
		printf("couldn't open test.txt\n");
		exit(-1);
	}

	
	for(j=0;j<5;j++)
	

	{
		fgets(s[j].name, 30, fp);

		if ( s[j].name[0] != '\0' )// string not empty
			s[j].name[ strlen(s[j].name) - 1 ] = '\0';// remove trailing newline (j.w.)
		printf("student name is %s\n\n", s[j].name);
		
		for(i=0;i<5;i++)
		{
			fscanf(fp, "%d\n", &s[j].marks[i] );
			printf(" mark %d\n\n", s[j].marks[i]);
		}
	}	

	//printf(" Student Name \t Average Mark\t Classification\n\n");

	//for(u=0;u<5;u++)
	//{
	//	ave= findmean(s[u].marks,5);
	
	//	printf(" %s \t %.0f\t\t\t",s[u].name, ave);
		
	
		
				
	
	//}

	
	fclose(fp);

}

for(j=0;j<5;j++)

[b]tmp.c[/b]:

#include <stdio.h>  

int main()
{
        int err;
        int i = 0, j;
        FILE *f;
 
        f = fopen("tmp_file", "r");
        if (!f) {
                perror("fopen");
                return -1;
        } else
                while ((j = fgetc(f)) && !feof(f))
                        printf("%c", j);
 
        return 0;
}

[b]tmp_file[/b]:

efefewf
efewgfrewgrwgrewgttrf
fewfrewrwer
1
2
4243
4324
3rwefre
frewrw
 
dswdw
 
erfewfrewftrw

thanks alot

but could you please explain it a little bit for me

thanks alot

but could you please explain it a little bit for me

Nope. I think that is enough help. I showed you exactly the type of loop you needed. Why don't you try looking at the man pages for all the functions you are unfamiliar with. You don't need to be coddled.

thanks again

i've had a look on some books and sites and i couldn't find anything like my problem i have completely no idea about how to change this code, i know i have to use feof with a while loop but do i have to change the structure that i've created and what other changes do i have to do.

i really need this program to work please help me :cry: :cry: :cry:

and thanks for your fast replies

When you need to read an unknown number of records, you have three choices:

1) Read and process one record at a time:

struct student stud;
int i;

while (fgets(stud.name, sizeof stud.name, in) != NULL) {
  for (i = 0; i < 5; i++) {
    if (fscanf(in, "%d", &stud.marks[i]) != 1)
      error("Invalid format");
  }

  process_record(stud);
}

2) Read and process a block of records:

struct student stud[5];
int i = 0, j;

while (fgets(stud[i].name, sizeof stud[i].name, in) != NULL) {
  for (i = 0; i < 5; i++) {
    if (fscanf(in, "%d", &stud[i].marks[i]) != 1)
      error("Invalid format");
  }

  if (++i == 5) {
    for (j = 0; j < 5; j++)
      process_record(stud[j]);

    i = 0;
  }
}

while (--i >= 0)
  process_record(stud[i]);

3) Use a dynamic data structure such as a linked list and read all of the records into it.

sorry

but could you please correct my code
please I'm very bad programmer and i do really need your help as soon as you can

thanks for your replies
hope any body con do this for me

thanks for your explainations

i really need your help :sad: :sad: :cry: :cry:

>please I'm very bad programmer
We don't care how good you are, therefore, that's not going to get you help any faster.

>but could you please correct my code
No. It's your code, and your problem, so you fix it. We'll help and offer suggestions, but if you expect someone to swoop in and solve all of your problems, you'll be disappointed for most of your life.

3) Use a dynamic data structure such as a linked list and read all of the records into it.

Which can lead to memory starvation if the amount of data is very large. So be careful using this technique.

sorry

but could you please correct my code
please I'm very bad programmer and i do really need your help as soon as you can

thanks for your replies
hope any body con do this for me

thanks for your explainations

i really need your help :sad: :sad: :cry: :cry:

I was about to do your homework for you... but 4 frowning smilies, two of which were crying, just wasn't convincing enough... sorry -- Have you asked your teacher for help? Perhaps they have office hours? Having someone there in person would greatly benefit you.

Write out your program in psuedo-code and post it here. Then, I will begin to help you. You need a clear understanding of the problem and you need to at least demonstrate this without the barrier of the semanatics.

Keep in mind that all you're doing in C is writing out a list of instructions for the CPU.

Which can lead to memory starvation if the amount of data is very large. So be careful using this technique.

and locality of reference could be poor

Thanks all

Thanks alot to all the people who helped me

Finally i got it sorted out and thanks again for the nice explainations.

:lol: :lol: :lol: :lol: :lol: :lol: :lol:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.