Experiment 12: Name: Soniya Rangari Section: B Branch: CSE Roll No: 117 Registration No: 20011028 Subject: DS1 (Lab)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Name : Soniya Rangari

Section : B
Branch : CSE
Roll No : 117
Registration No : 20011028
Subject : DS1(Lab)

Experiment 12
Aim of the Experiment : Write a C Program to create file for storing details of all the items needed for playing
various games of your choice also perform display operation, insertion of new item at any location, deletion of
any item

ALGORITHM

Step 1 : Create a structure cricket{

}crik;

Step 2 : Create a menu driven program and take choice from the user.
Case 1 : insert(); Case 2 :
display(); Case 3 : delete();
Case 4 : removefile(); Case 5 : exit(1);

Step 3 : Case 1 : INSERT()--->

3.1 Create FILE * fpand open file using fopen()


3.2 If(fp==Null) : display “Cant open”
3.3 Take input from the user.
3.4 Write the data in file using fwrite() and close file usingfclose()
Step 4 : Case 2 : DISPLAY()--->

4.1 Create FILE * fp1and open file using fopen()in


read mode
4.2 If(fp1==Null) : display “Cant open”
4.3 Readthecontentoffileusingfread()andprintit on theconsole.
4.4 Close file fclose()

Step 5 : Case 3 : DELETE()--->

5.1 Create two pointers FILE *fpo for original file and *fptemp for
temporary file and open original in read and temporary in write mode.

5.2 Take the input from user that which recordto be deleted.
5.3 Check if (crik.idx!=entered element) : writethe record in temporary file
using fwrite()
5.4 Close both files and open again but this time original inwrite and
temporary in read mode and perform
while (fread(&cri, sizeof(cri), 1, fptemp)){
fwrite(&cri, sizeof(cri), 1, fpo);
}
5.5 Close bothfiles.

Step 6 : Case 4 : REMOVEFILE()--->

To remove created files from memory.

Step7: Case 5 : EXIT().

Step 8: Stop.

LOGIC

To delete —>

● Open source file in r (read) mode. Store its reference in a FILE pointer variable fpo.
● Create and open a temporary file in w (write) mode. Store its
reference in a variable fptemp.
● Read word to remove from user in some variable in.
● Read the source file fpoexcept the element to be deleted and store it in a temporary file fptemp.
● Close both file fpoand fpTemp.
● Again open source file in w (write) mode. Store its reference in a FILE pointer variablefpo.
● Create and open a temporary file in r (read) mode. Store its reference
in a variable fptemp.
● Read the temporary file fptemp and store it in a source file fpo again
.
● Close both file fpoand fpTemp.
RAW CODE

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Cricket
{
int idx;
char equip[1000];
}cri;

void insert()
{
FILE *fp;
fp = fopen("D:\\Soniya\\Exp12.dat", "a");
if (fp == NULL){
fprintf(stderr, "\nError can't open file\n");
exit (1);
}
printf("Enter the equipment no :");
scanf("%d", &cri.idx);
printf("Enter the equipment name :");
scanf("%s", &cri.equip);
fwrite(&cri, sizeof(cri), 1, fp);
fclose(fp);
}

void display()
{
FILE *fp1;
fp1 = fopen("D:\\Adi\\Exp12.dat", "r");
if (fp1 == NULL){
fprintf(stderr, "\nError can't open file\n");
exit (1);
}
printf("\nEquipment No\tEquipment Name\n\n");
while (fread(&cri, sizeof(cri), 1, fp1)){
printf(" %d\t\t%s\n", cri.idx, cri.equip);
}
fclose(fp1);
}

void delete()
{
FILE *fpo ,*fptemp;
int in, ino;
printf("Enter the equipment number you want to delete :");
scanf("%d", &in);

fpo = fopen("D:\\Adi\\Exp12.dat", "r");


fptemp = fopen("D:\\Adi\\tr.dat", "w");
while (fread(&cri, sizeof(cri), 1, fpo))
{
ino = cri.idx;
if (ino != in){
fwrite(&cri, sizeof(cri), 1, fptemp);
}
}
fclose(fpo);
fclose(fptemp);
fpo = fopen("D:\\Adi\\Exp12.dat", "w");
fptemp = fopen("D:\\Adi\\tr.dat", "r");
while (fread(&cri, sizeof(cri), 1, fptemp)){
fwrite(&cri, sizeof(cri), 1, fpo);
}
printf("\n||Record Deleted successfully||\n");
fclose(fpo);
fclose(fptemp);
}

void removefile(){
remove("D:\\Adi\\Exp12.dat");
remove("D:\\Adi\\tr.dat");
}

int main(){
int choice;
printf("\n__FILE OPERATIONS__\n");
printf("\n\t1.Insert\n\t2.Display\n\t3.Delete\n\t4.Removefile\n\t5.Exit");
while(1){

printf("\n\tEnter your choice : ");


scanf("%d",&choice);
printf("\n");

switch(choice){
case 1 : insert();
break;
case 2 : display();
break;
case 3 : delete();
break;
case 4 : removefile();
break;
case 5 : printf("\nExiting the menu");
exit(1);
break;
}
}
return 0;
}
Code Snapshot :

STRUCT, INSERT AND DISPLAY


DELETE AND REMOVE FILE FUNCTION
MAIN PROGRAM
OUTPUT SNAPSHOT:
CONCLUSION

Hence we implemented the File operations using C language


In which we performed insert,display and delete functions successfully.

You might also like