Originally Posted by
aminpost
thx for your hot guide
but now can help me to solve these error(i think error is in call sort function)
Yes, we're not done with the sort function. Note the function definition (aka prototype), for sort() - you've changed it to pass an int, and 2 char's. Now look at the sort() function itself, and what is it being passed? An int - fine, and two arrays - but two arrays are not two char's!!! <eek!>
Our student array of structs is now global - so all functions can access it directly, without it being a passed in parameter. So there is no need for any parameters for sort(), at all.
If you do want student[] array of structs to be local, and to be passed to each function, then you need to:
1) Move the declaration of student array, into main(), and 2) pass a pointer to that array, to each function, because every function needs the student array.
So sort() could be a void function (as student array is now), or sort() could need a pointer to that array, if you move student[] array into main().
You decide.
Code:
#include <stdio.h>
#include <stdio.h>
struct studentinfo {
char name[20];
char fname[20];
int student_no[10];
}student[10];
void readData(void);
void showData(void);
void sort(int,char,char);
int main()
{
FILE *f;
char name[10][20],fname[10][20];
int student_no[10];
f=fopen("students.txt","w+");
for (int i=0 ; i<10 ; i++)
{
printf("\nEnter Name Of Student %d:",i+1);
scanf("%s",name[i]);
printf("Enter Family Name of Student %d:",i+1);
scanf("%s",fname[i]);
printf("Enter Student Number Of Student %d:",i+1);
scanf("%d",&student_no);
}
for (i=0 ; i<10 ; i++)
fprintf(f,"%s %s %d\n",name[i],fname[i],student_no[i]);
fclose(f);
readData();
showData();
sort(student[i].student_no,student[i].name,student[i].fname);
return 0;
}
void readData()
{
FILE *f;
f=fopen("student.txt","rb");
fread(student,sizeof student,1,f);
fclose(f);
}
void showData()
{
int k;
for(k=0 ; k<10 ; k++)
{
printf("%s",student[k].name);
printf("%s",student[k].fname);
printf("%s",student[k].student_no);
}
}
void sort(int student[].student_no,char student[].name,char student[].fname)
{
int i,j;
char tempname[10],tempfamily[10];
int tempNum[10];
FILE *p;
p=fopen("sortedStudents.txt","w+");
for(i = 0 ; i < 10 ; i++)
{
for(j=i+1 ; j<10 ; j++)
if(student[i].student_no > student[j].student_no)
{
tempNum=student[i].student_no;
tempname=student[i].name;
tempfamily=student[i].fname;
student[i].student_no=student[j].student_no;
student[i].name=student[j].name;
student[i].fname=student[j].fname;
student[j].student_no=tempNum;
student[j].name=tempname;
student[j].fname=tempfamily;
}
for(i=0 ; i<10 ; i++)
fprintf(p,"%s %s %d\n",name[i],fname[i],student_no[i]);
fclose(p);
}
First, please learn to indent your code properly. It helps you see errors in your code, very quickly, and it helps me and everyone else, see them as well. Our eyes just become trained to see things in your code, when you indent properly.
I use 3 spaces between levels of indentation, but anywhere between 2 and 6 spaces, is fine. Pick a number and be consistent. If you use spaces, then always use the space bar for making those spaces. If you use tabs, then set it for this, and always use tabs. The forum makes a dogs breakfast if you use tabs and mix in spaces, sometimes. Looks horrid.
Code:
void sort(int student[].student_no,char student[].name,char student[].fname)
{
int i,j;
char tempname[10],tempfamily[10];
int tempNum[10];
FILE *p;
p=fopen("sortedStudents.txt","w+");
/* You need to malloc some memory for tempname[], tempfamily[], and tempNum[]. The compiler
*may* give you "safe" memory as is, but that's just good luck. Don't count on it.
put the malloc
code, right here. */
//this is incorrect. make it i < 10 - 1; j goes to 9, i must stop at < 9. (8)
for(i = 0 ; i < 10 ; i++) //i stops at 8
{
for(j = i+1 ; j < 10 ; j++) //j stops at 9 (highest array element is 9)
{
if(student[i].student_no > student[j].student_no)
{
tempNum=student[i].student_no;
/* No way! You have to use strcpy(destination, source), to copy strings in C. */
tempname=student[i].name;
tempfamily=student[i].fname;
student[i].student_no=student[j].student_no;
/* same here - use strcpy */
student[i].name=student[j].name;
student[i].fname=student[j].fname;
student[j].student_no=tempNum;
/* and again - use strcpy */
student[j].name=tempname;
student[j].fname=tempfamily;
}
//You need two right facing brace in here, somewhere.
/* Look at the red curly braces {}. You have 4 facing left {, but only 2 facing right, }.
They must have an equal number. Now that the indentation is good, it's easy
to see that. */
//this needs to go *outside* the sorting loops, not inside them.
for(i=0 ; i<10 ; i++)
fprintf(p,"%s %s %d\n",name[i],fname[i],student_no[i]);
fclose(p);
}