Saturday, November 14, 2009

Chap12[C]d Sorting to alphabetical order in a file

Suppose a file contains student’s records with each record containing name and age of a student. Write a program to read these records and display them in sorted order by name.

Program to input students name
#include "stdio.h"

void main()
{
char j='y';
FILE *fp;

struct record
{
char name[20];
int age;
};
struct record a;

fp=fopen("c:\\ctxt\\record.txt","a");
if(fp==NULL)
{
puts("Cannot open file");
exit();
}
while(j=='y')
{
printf("\nKey in the name of the student\n");
scanf("%s",a.name);
printf("\nKey in the age of the student\n");
scanf("%d",&a.age);
fprintf(fp,"%s %d\n",a.name,a.age);
printf("\nAdd another record?(Y/N)");
fflush(stdin);
j=getche();
}
fclose(fp);
}

Program to sort students name


#include "stdio.h"
struct record
{
char name[10];
int age;
};
void main()
{
FILE *fp,*ft;

int i,f=1;

struct record a[5000];

clrscr();


fp=fopen("c:\\ctxt\\record.txt","r");
if(fp==NULL)
{
puts("Cannot open file");
exit();
}
for(i=0;i<5000;i++)
{
if(f==1)
{
if((fscanf(fp,"%s%d",a[i].name,&a[i].age)==EOF))
f=0;
}
if(f==0)
a[i].name[0]='\0';

}

funcsort(a,4);

for(i=0;a[i].name[0]!='\0';i++)
printf("\nName is %s. Age is %d",a[i].name,a[i].age);
printf("\nFile sorted according to alphabetical order");

ft=fopen("c:\\ctxt\\record2.txt","w");
for(i=0;a[i].name[0]!=0;i++)
fprintf(ft,"%s %d\n",a[i].name,a[i].age);
fclose(fp);
fclose(ft);
remove("c:\\ctxt\\record.txt");
rename("c:\\ctxt\\record2.txt","c:\\ctxt\\record.txt");

}
/* Function "funcsort()" is in picture below type it in for it to work */

5 comments:

  1. here you are entering data by structure but its written "suppose a file contain student's record...."

    ReplyDelete
    Replies
    1. Records are created \updated \deleted through structures.

      Delete
  2. VIJAYNITJ-->>
    //THIS PROGRAM WRITE RECORDS IN A FILE AND THEN READ IT RECORDS IN SORTED ORDER BY NAMES IN RECORD
    //AND DISPLAY THEM ON SCREEN
    #include
    #include
    #include
    int main()
    {
    FILE *fw,*fr;
    char opsn='y';
    char *p;
    int count=0;
    //for writing records into file
    struct student
    {
    char name[20];
    int age;
    }s;

    fw=fopen("op.txt","w");
    while(opsn=='y')
    {
    count++;
    printf("Enter student name and age\n");
    scanf("%s%d",s.name,&s.age);
    fprintf(fw,"%3s%3d\n",s.name,s.age);
    printf("Wanna enter another record(y|n)\n");
    fflush(stdin);
    opsn=getche();
    }
    fclose(fw);
    //for reading records rom file
    fr=fopen("op.txt","r");
    struct st
    {
    char nm[20];
    int ag;
    }s2[count];

    printf("\nRecord from file is\n");
    int i=0,j;
    char temp[20];
    while(fscanf(fr,"%s%d",s.name,&s.age)!=EOF)
    {
    strcpy(s2[i].nm,s.name);
    s2[i].ag=s.age;
    i++;
    }
    // for sorting records
    for(i=0;i0)
    {
    strcpy(temp,s2[i].nm);
    strcpy(s2[i].nm,s2[j].nm);
    strcpy(s2[j].nm,temp);
    }}}
    //for displaying records
    for(i=0;i<count;i++)
    printf("\n%s\t\t%d",s2[i].nm,s2[i].ag);
    fclose(fr);
    }

    ReplyDelete
    Replies
    1. #include
      #include
      #include
      #include
      int main()
      {
      FILE *fw,*fr;
      char opsn='y';
      char *p;
      int count=0;
      //for writing records into file
      struct student
      {
      char name[20];
      int age;
      }s;

      fw=fopen("op.txt","w");
      while(opsn=='y')
      {
      count++;
      printf("Enter student name and age\n");
      scanf("%s%d",s.name,&s.age);
      fprintf(fw,"%3s%3d\n",s.name,s.age);
      printf("Wanna enter another record(y|n)\n");
      fflush(stdin);
      opsn=getche();
      }
      fclose(fw);
      //for reading records rom file
      fr=fopen("op.txt","r");
      struct st
      {
      char nm[20];
      int ag;
      }s2[count];

      printf("\nRecord from file is\n");
      int i=0,j;
      char temp[20];
      while(fscanf(fr,"%s%d",s.name,&s.age)!=EOF)
      {
      strcpy(s2[i].nm,s.name);
      s2[i].ag=s.age;
      i++;
      }
      // for sorting records
      for(i=0;i<count; i++)
      {
      strcpy(temp,s2[i].nm);
      strcpy(s2[i].nm,s2[j].nm);
      strcpy(s2[j].nm,temp);
      }
      //for displaying records
      for(i=0;i<count;i++)
      printf("\n%s\t\t%d",s2[i].nm,s2[i].ag);
      fclose(fr);
      }

      Delete