C Program to Sort the numbers and store them in file using user define function
C Program to Sort the numbers and store them in file using user define function
#include
int bubble(int*,int);
void filewrite();
void avgmarks();
void fileprint();
void filesort();
void rollin();
/*********************** SORTING FUNCTION ***************************/
int bubble(int x[],int n)
{
int hold,j,pass,i,switched = 1;
for(pass = 0; pass < n-1 && switched == 1;pass++) { switched=0; for (j=0;jx[j+1])
{
switched=1;
hold = x[j];
x[j] = x[j+1];
x[j+1]=hold;
}
}
return(0);
}
/*********************** FILE WRITING FUNCTION ******************************/
void filewrite()
{
int roll,ch,mark;
char nam[50];
FILE *fp;
clrscr();
fp = fopen("student.txt","a");
printf("ENTER ROLL NUMBER, NAME , MARKS \n");
ch =1;
while(ch)
{
scanf("%d%s%d",&roll,&nam,&mark);
fprintf(fp,"%d %s %d\n",roll,nam,mark);
printf("\n\n press 1 to continue,0 to stop");
scanf("%d",&ch);
}
fclose(fp) ;
}
/******************** OUTPUTING DATA ON SCREEN***************/
void fileprint()
{
int marks[100],rollno[100],x[100],i;
char name[100][50];
FILE *fp;
clrscr();
fp = fopen("student.txt","r");
i=0;
printf("ROLLNO NAME MARK\n");
while(!feof(fp))
{
fscanf(fp,"%d %s %d\n",&rollno[i],&name[i],&marks[i]);
printf(" %d %s %d\n",rollno[i],name[i],marks[i]);
i=i+1;
}
fclose(fp);
printf("\n\n\nPRESS ANY KEY");
getch();
}
/******************* SORTING FILE ************************/
void filesort()
{ int marks[100],rollno[100],x[100],n,i,j;
char name[100][50];
FILE *fp,*fm;
fp = fopen("student.txt","r");
fm = fopen("marks.txt","w");
i=0;
while(! feof(fp))
{
fscanf(fp,"%d %s %d\n",&rollno[i],&name[i],&marks[i]);
x[i]= marks[i];
i=i+1;
}
n=i;
bubble(x,n);
for(i=0;i
No comments:
Post a Comment