Thursday 14 June 2012

To create a file for insert,display,delete,update,sort records

#include<stdio.h>
struct student
{
int rollno;
char name[30];
float mark;
}stud;
//    FUNCTION TO INSERT RECORDS TO THE FILE
void insert()
{
FILE *fp;
fp = fopen("Record", "a");
printf("Enter the Roll no   :");
scanf("%d", &stud.rollno);
printf("Enter the Name      :");
scanf("%s", &stud.name);
printf("Enter the mark      :");
scanf("%f", &stud.mark);
fwrite(&stud, sizeof(stud), 1, fp);
fclose(fp);
}
//    FUNCTION TO DISPLAY RECORDS
void disp()
{
FILE *fp1;
fp1 = fopen("Record", "r");
printf("\nRoll Number\tName\tMark\n\n");
while (fread(&stud, sizeof(stud), 1, fp1))
printf("  %d\t\t%s\t%.2f\n", stud.rollno, stud.name, stud.mark);
fclose(fp1);
}
//    FUNCTION TO SEARCH THE GIVEN RECORD
void search()
{
FILE *fp2;
int r, s, avl;
printf("\nEnter the Roll no you want to search  :");
scanf("%d", &r);
avl = avlrollno(r);
if (avl == 0)
printf("Roll No %d is not available in the file\n",r);
else
{
fp2 = fopen("Record", "r");
while (fread(&stud, sizeof(stud), 1, fp2))
{
s = stud.rollno;
if (s == r)
{
printf("\nRoll no = %d", stud.rollno);
printf("\nName    = %s", stud.name);
printf("\nMark    = %.2f\n", stud.mark);
}
}
fclose(fp2);
}
}
//    FUNCTION TO DELETE A RECORD


void deletefile()
{
FILE *fpo;
FILE *fpt;
int r, s;
printf("Enter the Roll no you want to delete :");
scanf("%d", &r);
if (avlrollno(r) == 0)
printf("Roll no %d is not available in the file\n", r);
else
{
fpo = fopen("Record", "r");
fpt = fopen("TempFile", "w");
while (fread(&stud, sizeof(stud), 1, fpo))
{
s = stud.rollno;
if (s != r)
fwrite(&stud, sizeof(stud), 1, fpt);
}
fclose(fpo);
fclose(fpt);
fpo = fopen("Record", "w");
fpt = fopen("TempFile", "r");
while (fread(&stud, sizeof(stud), 1, fpt))
fwrite(&stud, sizeof(stud), 1, fpo);
printf("\nRECORD DELETED\n");
fclose(fpo);
fclose(fpt);
}

}
//    FUNCTION TO UPDATE THE RECORD
void update()
{
int avl;
FILE *fpt;
FILE *fpo;
int s, r, ch;
printf("Enter roll number to update:");
scanf("%d", &r);
avl = avlrollno(r);
if (avl == 0)
{
printf("Roll number %d is not Available in the file", r);
}
else
{
fpo = fopen("Record", "r");
fpt = fopen("TempFile", "w");
while (fread(&stud, sizeof(stud), 1, fpo))
{
s = stud.rollno;
if (s != r)
fwrite(&stud, sizeof(stud), 1, fpt);
else
{
printf("\n\t1. Update Name of Roll Number %d", r);
printf("\n\t2. Update Mark of Roll Number %d", r);
printf("\n\t3. Update both Name and Mark of Roll Number %d", r);
printf("\nEnter your choice:");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter Name:");
scanf("%s", &stud.name);
break;
case 2:
printf("Enter Mark : ");
scanf("%f", &stud.mark);
break;
case 3:
printf("Enter Name: ");
scanf("%s", &stud.name);
printf("Enter Mark: ");
scanf("%f", &stud.mark);
break;
default:
printf("Invalid Selection");
break;
}
fwrite(&stud, sizeof(stud), 1, fpt);
}
}
fclose(fpo);
fclose(fpt);
fpo = fopen("Record", "w");
fpt = fopen("TempFile", "r");
while (fread(&stud, sizeof(stud), 1, fpt))
{
fwrite(&stud, sizeof(stud), 1, fpo);
}
fclose(fpo);
fclose(fpt);
printf("RECORD UPDATED");
}
}
/* FUNCTION TO SORT THE RECORD */
void sort()
{
int a[20], count = 0, i, j, t, c;
FILE *fpo;
fpo = fopen("Record", "r");
while (fread(&stud, sizeof(stud), 1, fpo))
{
a[count] = stud.rollno;
count++;
}
c = count;
for (i = 0; i<count - 1; i++)
{
for (j = i + 1; j<count; j++)
{
if (a[i]>a[j])
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
printf("Roll No.\tName\t\tMark\n\n");
count = c;
for (i = 0; i<count; i++)
{
rewind(fpo);
while (fread(&stud, sizeof(stud), 1, fpo))
{
if (a[i] == stud.rollno)
printf("\n %d\t\t %s \t\t %2f",stud.rollno, stud.name, stud.mark);
}

}
}
//    FUNCTION TO CHECK GIVEN ROLL NO IS AVAILABLE //
int avlrollno(int rno)
{
FILE *fp;
int c = 0;
fp = fopen("Record", "r");
while (!feof(fp))
{
fread(&stud, sizeof(stud), 1, fp);

if (rno == stud.rollno)
{
fclose(fp);
return 1;
}
}
fclose(fp);
return 0;
}
//FUNCTION TO CHECK THE FILE IS EMPTY OR NOT
int empty()
{
int c = 0;
FILE *fp;
fp = fopen("Record", "r");
while (fread(&stud, sizeof(stud), 1, fp))
c = 1;
fclose(fp);
return c;
}
// MAIN PROGRAM
void main()
{
int c, emp;
do
{
printf("\n\t---Select your choice---------\n");
printf("\n\t1. INSERT\n\t2. DISPLAY\n\t3. SEARCH");
printf("\n\t4. DELETE\n\t5. UPDATE\n\t6. SORT");
printf("\n\t7. EXIT");
printf("\n\n------------------------------------------\n");
printf("\nEnter your choice:");
scanf("%d", &c);
printf("\n");
switch (c)
{
case 1:
insert();
break;
case 2:
emp = empty();
if (emp == 0)
printf("\nThe file is EMPTY\n");
else
disp();
break;
case 3:
search();
break;
case 4:
deletefile();
break;
case 5:
update();
break;
case 6:
emp = empty();
if (emp == 0)
printf("\n The file is EMPTY\n");
else
sort();
break;
case 7:
exit(1);
break;
default:
printf("\nYour choice is wrong\nPlease try again...\n");
break;

}
} while (c != 7);


}

Out put





32 comments:

  1. please solve the errors in this program

    ReplyDelete
    Replies
    1. as salam. sir, i am a student. i have some problem about c pro. our teacher, tell us add data, delete data, update.. search.. and sorting data use array. i can do all that operation separately, but i can't understand how can i do all that operation in a single program. please help me sir please.

      Delete
    2. Salam Dear. this program will do all of the function
      add data, delete data, update.. search.. and sorting data. run this program you can see it.

      Delete
    3. Thank you sir, Thank you. Thanks a lot.
      I really appreciate .

      Delete
  2. pal this code is not working

    ReplyDelete
  3. Get your codes working, bruh.

    ReplyDelete
  4. yow the program na wuk fix the dammn thing or tek it dung!!!!!!!!!!!

    ReplyDelete
  5. solve the errors .... and why u have used avlrollno() ????

    ReplyDelete
    Replies
    1. i have updated, check it. avlrollno() is used for searching

      Delete
  6. hiii

    plz specify (stdlib.h) header file & specify avlrollno(int)before structure then program run properly

    ReplyDelete
  7. pls i can i convert the search function to use stud.name to search the file.

    ReplyDelete
  8. if you know how to debug you can fix the errors in that program.. and thanks to shahul.. the program was fantastic tho there are some minor errors.

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. sir this code allows to enter same Roll n any n. of times,,,and Roll n. did not accept character with digit.

    ReplyDelete
  11. Hi All when I run the program it's working. if you getting any error during compilation please let me know. You can post the screen shot and I will try to help

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. Hi every one plzz help me when i compile this code that this [Error] 'avlrollno' was not declared in this scope) generated solve it please

    ReplyDelete
  14. how about id number instead of roll no

    ReplyDelete
  15. Use the code as reference and do not copy paste people!!! Please learn

    ReplyDelete
  16. [Error] 'avlrollno' was not declared in this scope
    [Error] '::main' must return 'int'

    ReplyDelete