PROGRAM-1
Program for system calls of unix operating systems (opendir, readdir,
closedir)
#include<stdio.h>
#include<dirent.h>
struct dirent *dptr;
int main(int argc, char *argv[])
{
char buff[100];
DIR *dirp;
printf("\n This output is produced by Aarish Saifi");
printf("\n enter the directory name");
scanf("%s", buff);
if((dirp=opendir(buff))==NULL)
{
printf("the given dircetroy does not eixt");
}
while(dptr=readdir(dirp))
{
printf("\n,%s dptr->d_name");
}
closedir(dirp);
}
1
OUTPUT:
2
PROGRAM-2
Write a Shell program to check the given number is even or odd.
echo "Enter the Number"
echo “This output is produced by Aarish Saifi”
read n
r=`expr $n % 2`
if [ $r -eq 0 ]
then
echo "$n is Even number"
else echo "$n is Odd number"
fi
OUTPUT:
3
PROGRAM-3
Write a Shell program to check the given year is leap year or not.
echo "Enter the year"
echo “This output is produced by Aarish Saifi”
read y b=`expr $y % 4`
if [ $b -eq 0 ]
then
echo "$y is a leap year"
else
echo "$y is not a leap year"
fi
OUTPUT:
4
PROGRAM-4
Write a Shell program to find the factorial of a number.
echo "Enter a Number"
echo “This output is produced by Aarish Saifi”
read n i=`expr $n - 1`
p=1
while [ $i -ge 1 ]
do
n=`expr $n \* $i`
i=`expr $i - 1`
done
echo "The Factorial of the given Number is $n”
OUTPUT:
5
PROGRAM-5
Write a Shell program to swap the two integers.
echo "Enter Two Numbers"
echo “This output is produced by Aarish Saifi”
read a b
temp=$a
a=$b
b=$temp
echo "after swapping"
echo $a $b
OUTPUT:
6
PROGRAM-6
Write a C program for implementation of Priority scheduling
algorithms.
#include<stdio.h>
#include<stdlib.h>
typedef struct {
int pno;
int pri;
int btime;
int wtime;
} Process;
int main() {
int i, j, n;
int tbm = 0, totwtime = 0, totttime = 0;
Process *p, t;
printf(“This output is produced by ”);
printf("\nPriority Scheduling\n");
printf("\nEnter the number of processes: ");
scanf("%d", &n);
p = (Process*)malloc(n * sizeof(Process));
printf("Enter the burst time and priority:\n");
for (i = 0; i < n; i++) {
printf("Process %d: ", i + 1);
scanf("%d %d", &p[i].btime, &p[i].pri);
p[i].pno = i + 1;
p[i].wtime = 0;
}
7
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (p[i].pri > p[j].pri) {
t = p[i];
p[i] = p[j];
p[j] = t;
}
}
}
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (i = 0; i < n; i++) {
totwtime += p[i].wtime = tbm;
tbm += p[i].btime;
printf("%d\t%d\t\t%d\t\t%d\n", p[i].pno, p[i].btime, p[i].wtime, p[i].wtime +
p[i].btime);
}
totttime = tbm + totwtime
printf("\nTotal Waiting Time: %d", totwtime);
printf("\nAverage Waiting Time: %f", (float)totwtime / n);
printf("\nTotal Turnaround Time: %d", totttime);
printf("\nAverage Turnaround Time: %f", (float)totttime / n);
free(p);
return 0;
}
8
OUTPUT:
9
PROGRAM-7
Write a c program for implementation of Round Robin scheduling
algorithms.
#include<stdio.h>
int main()
{
int n;
printf("Enter Total Number of Processes:");
printf("\n This output is produced by Aarish Saifi");
scanf("%d", &n);
int wait_time = 0, ta_time = 0, arr_time[n], burst_time[n],
temp_burst_time[n];
int x = n;
for(int i = 0; i < n; i++)
{
printf("Enter Details of Process %d \n", i + 1);
printf("Arrival Time: ");
scanf("%d", &arr_time[i]);
printf("Burst Time: ");
scanf("%d", &burst_time[i]);
temp_burst_time[i] = burst_time[i];
}
int time_slot;
printf("Enter Time Slot:");
scanf("%d", &time_slot);
int total = 0, counter = 0,i;
printf("Process ID Burst Time Turnaround Time Waiting Time\n");
10
for(total=0, i = 0; x!=0; )
{
if(temp_burst_time[i] <= time_slot && temp_burst_time[i] > 0)
{
total = total + temp_burst_time[i];
temp_burst_time[i] = 0;
counter=1;
}
else if(temp_burst_time[i] > 0)
{
temp_burst_time[i] = temp_burst_time[i] - time_slot;
total += time_slot;
}
if(temp_burst_time[i]==0 && counter==1)
{
x--;
printf("\nProcess No %d \t\t %d\t\t\t\t %d\t\t\t %d", i+1, burst_time[i],
total-arr_time[i], total-arr_time[i]-burst_time[i]);
wait_time = wait_time+total-arr_time[i]-burst_time[i];
ta_time += total -arr_time[i];
counter =0;
}
if(i==n-1)
{
i=0;
}
else if(arr_time[i+1]<=total)
11
{
i++;
}
else
{
i=0;
}
}
float average_wait_time = wait_time * 1.0 / n;
float average_turnaround_time = ta_time * 1.0 / n;
printf("\nAverage Waiting Time:%f", average_wait_time);
printf("\nAvg Turnaround Time:%f", average_turnaround_time);
return 0;}
OUTPUT:
12
PROGRAM-8
Write a C program for implementation of FCFS and SJF scheduling
algorithms
#include<stdio.h>
#include<stdlib.h>
struct fcfs {
int pid;
int btime;
int wtime;
int ttime;
};
int main() {
int i, n;
int totwtime = 0, totttime = 0;
printf("\nFCFS Scheduling\n");
printf("\n This output is produced by Aarish Saifi");
printf("Enter the number of processes: ");
scanf("%d", &n);
struct fcfs p[n];
for (i = 0; i < n; i++) {
p[i].pid = i + 1;
printf("Enter the burst time for process %d: ", p[i].pid);
scanf("%d", &p[i].btime);
13
}
p[0].wtime = 0;
p[0].ttime = p[0].btime;
totttime += p[0].ttime;
for (i = 1; i < n; i++) {
p[i].wtime = p[i - 1].wtime + p[i - 1].btime;
p[i].ttime = p[i].wtime + p[i].btime;
totttime += p[i].ttime;
totwtime += p[i].wtime;
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\n", p[i].pid, p[i].btime, p[i].wtime, p[i].ttime);
printf("\nTotal Waiting Time: %d\n", totwtime);
printf("Average Waiting Time: %.2f\n", (float)totwtime / n);
printf("Total Turnaround Time: %d\n", totttime);
printf("Average Turnaround Time: %.2f\n", (float)totttime / n);
return 0;
14
OUTPUT:
15
PROGRAM-9
Write a C program for implementation of SJF scheduling algorithms.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
typedef struct {
int pid;
int btime;
int wtime;
} sp;
int main() {
int i, j, n, tbm = 0, totwtime = 0, totttime = 0;
sp *p, t;
printf("\nSJF Scheduling\n");
printf("\n This output is produced by Aarish Saifi");
printf("Enter the number of processes: ");scanf("%d", &n);
p = (sp*)malloc(n * sizeof(sp));
printf("Enter the burst time for each process:\n");
for (i = 0; i < n; i++) {
printf("Process %d: ", i + 1);
scanf("%d", &p[i].btime);
p[i].pid = i + 1;
p[i].wtime = 0;
16
}
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (p[i].btime > p[j].btime) {
t = p[i];
p[i] = p[j];
p[j] = t;
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (i = 0; i < n; i++) {
p[i].wtime = tbm;
tbm += p[i].btime;
totwtime += p[i].wtime;
totttime += tbm;
printf("%d\t%d\t\t%d\t\t%d\n", p[i].pid, p[i].btime, p[i].wtime, tbm);
printf("\nTotal Waiting Time: %d\n", totwtime);
printf("Average Waiting Time: %.2f\n", (float)totwtime / n);
printf("Total Turnaround Time: %d\n", totttime);
printf("Average Turnaround Time: %.2f\n", (float)totttime / n);
return 0;}
17
OUTPUT:
18
PROGRAM-10
Write a C-program to implement the producer – consumer problem
using semaphores.
#include <stdio.h>
#include <stdlib.h>
int mutex = 1, full = 0, empty = 3, x = 0;
int wait(int);
int signal(int);
void producer();
void consumer();
int main()
{
int n;
printf("\n This output is produced by Aarish Saifi");
printf("1. Producer\n2. Consumer\n3. Exit\n");
while (1)
{
printf("Enter your choice: ");
scanf("%d", &n);
switch (n)
{
case 1:
if (mutex == 1 && empty != 0)
producer();
else
printf("Buffer is full.\n");
19
break;
case 2:
if (mutex == 1 && full != 0)
consumer();
else
16
printf("Buffer is empty.\n");
break;
case 3:
exit(0);
break;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
int wait(int s)
{
return (--s);
}
int signal(int s)
{
return (++s);
}
void producer()
{mutex = wait(mutex);
20
full = signal(full);
empty = wait(empty);
x++;
printf("Producer produces item %d\n", x);
mutex = signal(mutex);
}
void consumer()
{
mutex = wait(mutex);
full = wait(full);
empty = signal(empty);
printf("Consumer consumes item %d\n", x);
x--;
mutex = signal(mutex);}
OUTPUT:
21
PROGRAM-11
Write a c program to implement IPC using shared memory.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#define SEG_SIZE 100
int main(int argc, char *argv[]) {
int shmid, cntr;
key_t key;
char *segptr;
char buff[] = "poooda......";
key = ftok(".", 's');
if ((shmid = shmget(key, SEG_SIZE, IPC_CREAT | IPC_EXCL | 0666)) == -1)
{
if ((shmid = shmget(key, SEG_SIZE, 0)) == -1) {
perror("shmget");
exit(1);
}
} else {
printf("\n This output is produced by Aarish Saifi");
printf("Creating a new shared memory segment\n");
printf("shmid: %d\n", shmid);
}
22
system("ipcs -m");
if ((segptr = (char *)shmat(shmid, 0, 0)) == (char *)-1) {
perror("shmat");
exit(1);
}
printf("Writing data to shared memory...\n");
strcpy(segptr, buff);
printf("Done\n");
printf("Reading data from shared memory...\n");
printf("Data: %s\n", segptr);
printf("Done\n");
printf("Removing shared memory segment...\n");
if (shmctl(shmid, IPC_RMID, 0) == -1)
printf("Can't remove shared memory segment\n");
else
printf("Removed successfully\n");
return 0;
}
OUTPUT:
23
PROGRAM-12
Write a C program to implement banker ‟s algorithm for deadlock
avoidance.
#include<stdio.h>
#include<stdlib.h>int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n, r;
void input();
void show();
void cal();
int main()
{
printf("********** Baner's Algorithm ************\n");
printf("\n This output is produced by Aarish Saifi");
input();
show();
cal();
return 0;
}
void input()
{
int i, j;
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the number of resource instances: ");
24
scanf("%d", &r);
printf("Enter the max matrix:\n");
for(i = 0; i < n; i++)
{
for(j = 0; j < r; j++)
{
scanf("%d", &max[i][j]);
}
}
printf("Enter the allocation matrix:\n");
for(i = 0; i < n; i++)
{
for(j = 0; j < r; j++)
{
scanf("%d", &alloc[i][j]);
}
}
printf("Enter the available resources:\n");
for(j = 0; j < r; j++)
{
scanf("%d", &avail[j]);
}
}
void show()
{
int i, j;
printf("\nProcess\tAllocation\tMax\tAvailable\n");
25
for(i = 0; i < n; i++)
{
printf("P%d\t", i + 1);
for(j = 0; j < r; j++)
{
printf("%d ", alloc[i][j]);
}
printf("\t");
for(j = 0; j < r; j++)
{
printf("%d ", max[i][j]);
}
printf("\t");
if(i == 0)
{
for(j = 0; j < r; j++)
printf("%d ", avail[j]);
}
printf("\n");
}
}
void cal()
{
int finish[100], temp, need[100][100], flag = 1, k, c1 = 0;
int safe[100];
int i, j;
for(i = 0; i < n; i++)
26
{
finish[i] = 0;
}
for(i = 0; i < n; i++)
{
for(j = 0; j < r; j++)
{
need[i][j] = max[i][j] - alloc[i][j];
}
}
printf("\n");
while(flag)
{
flag = 0;
for(i = 0; i < n; i++)
{
int c = 0;
for(j = 0; j < r; j++)
{
if((finish[i] == 0) && (need[i][j] <= avail[j]))
{
c++;
if(c == r)
{
for(k = 0; k < r; k++)
{
avail[k] += alloc[i][k];
27
}
finish[i] = 1;
flag = 1;
printf("P%d -> ", i);
if(finish[i] == 1)
{
i = n;
}
}
}
}
}
}
for(i = 0; i < n; i++)
{
if(finish[i] == 1)
{
c1++;
}
else
{
printf("P%d -> ", i);
}
}
if(c1 == n)
{
28
printf("\nThe system is in a safe state.\n");
}
else
{
printf("\nThe system is not in a safe state.\n");
}
OUTPUT:
29
PROGRAM-13
Write a C program to implement algorithm for deadlockdetection.
#include<stdio.h>
#include<conio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n,r;
void input();
void show();
void cal();
int main()
{
int i,j;
printf("********** Deadlock Detection Algo ************\n");
printf("\n This output is produced by Aarish Saifi");
input();
show();
cal();
getch();
return 0;
}
void input()
{int i,j;
printf("Enter the no of Processes\t");
30
scanf("%d",&n);
printf("Enter the no of resource instances\t");
scanf("%d",&r);
printf("Enter the Max Matrix\n");
for(i=0;i<n;i++)
{for(j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}}
printf("Enter the Allocation Matrix\n");
for(i=0;i<n;i++)
{for(j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
}}
printf("Enter the available Resources\n");
for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
}}
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t Available\t");
for(i=0;i<n;i++)
{
printf("\nP%d\t ",i+1);
31
for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t");
for(j=0;j<r;j++)
{printf("%d ",max[i][j]);
}
printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",avail[j]);
}}}
void cal()
{ int finish[100],temp,need[100][100],flag=1,k,c1=0;
int dead[100];
int safe[100];
int i,j;
for(i=0;i<n;i++)
{finish[i]=0;
}
//find need matrix
for(i=0;i<n;i++)
{for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
32
}}
while(flag)
{flag=0;
for(i=0;i<n;i++)
{int c=0;
for(j=0;j<r;j++)
{if((finish[i]==0)&&(need[i][j]<=avail[j]))
{c++;
if(c==r)
{
for(k=0;k<r;k++)
{avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
}
if(finish[i]==1)
{i=n;
}}}}}}
j=0;
flag=0;
for(i=0;i<n;i++)
{
if(finish[i]==0)
{dead[j]=i;
j++;
flag=1;
}}
33
if(flag==1)
{
printf("\n\nSystem is in Deadlock and the Deadlock process are\n");
for(i=0;i<n;i++)
{printf("P%d\t",dead[i]);
}}
else
{
printf("\nNo Deadlock Occur"); }}
OUTPUT:
34
PROGRAM-14
Write a c program to implement Threading and Synchronization
Applications.
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
void* dosomething(void *arg) {
unsigned long i = 0;
pthread_t id = pthread_self();
if(pthread_equal(id, tid[0])) {
printf("\nFirst thread processing\n");
}
else {
printf("\nSecond thread processing\n");
}
for(i=0; i<(0xffffffff); i++);
return NULL;
}
int main(void) {
int i = 0;
int err;
while(i < 2) {
err = pthread_create(&(tid[i]), NULL, &dosomething, NULL);
if (err != 0)
35
printf("\nCan't create thread :[%s]", strerror(err));
else
printf("\nThread created successfully\n");
i++;
}
31
sleep(5);
return 0;
}
OUTPUT:
PROGRAM-15
36
Write a c program for implementation memory allocation methods for
fixed partition using first fit.
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
printf("\n This output is produced by Aarish Saifi");
printf("\n\tMemory Management Scheme - Worst Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
37
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
}
}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
OUTPUT:
38
PROGRAM-16
39
Write a c program to implement Paging technique for memory
management.
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_t tid[2];
int counter;
pthread_mutex_t lock;
void* doSomeThing(void *arg)
{
pthread_mutex_lock(&lock);
unsigned long i = 0;
counter += 1;
printf("\n This output is produced by Aarish Saifi");
printf("\n Job %d started\n", counter);
for(i=0; i<(0xFFFFFFFF);i++);
printf("\n Job %d finished\n", counter);
pthread_mutex_unlock(&lock);
return NULL;
}
int main(void)
{
int i = 0;
int err;
if (pthread_mutex_init(&lock, NULL) != 0)
40
{ printf("\n mutex init failed\n");
return 1;
}
while(i < 2)
{
err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
if (err != 0)
printf("\ncan't create thread :[%s]", strerror(err));
i++;
}
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
pthread_mutex_destroy(&lock);
return 0;}
OUTPUT:
41
PROGRAM-17
Write a C program for implementation of FIFO page
42
replacement algorithm
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <graphics.h>
void main()
{
int gd = DETECT, gm, count, i, j, mid, cir_x;
char fname[10][20];
clrscr();
initgraph(&gd, &gm, "c:/tc/bgi");cleardevice();
setbkcolor(GREEN);
printf("\n This output is produced by Aarish Saifi");
printf("Enter the number of files: ");
scanf("%d", &count);
if (count > 0 && count <= 10)
{
cleardevice();
setbkcolor(GREEN);
for (i = 0; i < count; i++)
{
printf("Enter file name %d: ", i + 1);
scanf("%s", fname[i]);
}
setfillstyle(1, MAGENTA);
mid = 640 / count;
cir_x = mid / 3;
43
bar3d(270, 100, 370, 150, 0, 0);
settextstyle(2, 0, 4);
settextjustify(1, 1);
outtextxy(320, 125, "root directory");
setcolor(BLUE);
for (i = 0; i < count; i++, cir_x += mid)
{
line(320, 150, cir_x, 250);
fillellipse(cir_x, 250, 30, 30);
outtextxy(cir_x, 250, fname[i]);
}
}
else
{
printf("Invalid number of files. )
}
OUTPUT:
PROGRAM-18
Write C program to organize the file using single level
44
directory.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FILES 10
#define MAX_FILENAME 20
struct File {
char name[MAX_FILENAME];
};
struct Directory {
struct File files[MAX_FILES];
int count;
};
void createFile(struct Directory* dir, const char* filename) {
if (dir->count >= MAX_FILES) {
printf("Directory is full. Cannot create file.\n");
return;
}
for (int i = 0; i < dir->count; i++) {
if (strcmp(dir->files[i].name, filename) == 0) {
printf("File '%s' already exists.\n", filename);
return;
}
}
struct File newFile;
strcpy([Link], filename);
45
dir->files[dir->count++] = newFile;
printf("File '%s' created successfully.\n", filename);
}
void deleteFile(struct Directory* dir, const char* filename) {
for (int i = 0; i < dir->count; i++) {
if (strcmp(dir->files[i].name, filename) == 0) {
for (int j = i; j < dir->count - 1; j++) {
dir->files[j] = dir->files[j + 1];
}
dir->count--;
printf("File '%s' deleted successfully.\n", filename);
return;
}
}
printf("File '%s' not found.\n", filename);
}
void displayFiles(const struct Directory* dir) {
printf("Files in the directory:\n");
for (int i = 0; i < dir->count; i++) {
printf("- %s\n", dir->files[i].name);
}
}
int main() {
struct Directory directory;
[Link] = 0;
int choice;
char filename[MAX_FILENAME];while (1) {
46
printf("\n-------------------------\n");
printf("1. Create file\n");
printf("2. Delete file\n");
printf("3. Display files\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the filename: ");
scanf("%s", filename);
createFile(&directory, filename);
break;
case 2:
printf("Enter the filename: ");
scanf("%s", filename);
deleteFile(&directory, filename);
break;
case 3:
displayFiles(&directory);
break;
case 4:
exit(0);
default:
printf("Invalid choice. Try again.\n");
}
} return 0;
47
}
OUTPUT:
PROGRAM-19
Write c program to organize the file using two level directory.
48
#include<stdio.h>
#include<conio.h>
struct st{
char dname[10];
char sdname[10][10];
char fname[10][10][10];
int ds,sds[10];
}dir[10];
void main(){
int i,j,k,n;
clrscr();
printf("\n This output is produced by Aarish Saifi");
printf("enter number of directories:");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("enter directory %d names:",i+1);
scanf("%s",&dir[i].dname);
printf("enter size of directories:");
scanf("%d",&dir[i].ds);
for(j=0;j<dir[i].ds;j++){
printf("enter subdirectory name and size:");
scanf("%s",&dir[i].sdname[j]);
scanf("%d",&dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++){
printf("enter file name:");
scanf("%s",&dir[i].fname[j][k]);}}}
printf("\ndirname\t\tsize\tsubdirname\tsize\tfiles");
49
printf("\n**\n");
for(i=0;i<n;i++){
printf("%s\t\t%d",dir[i].dname,dir[i].ds);
for(j=0;j<dir[i].ds;j++){
printf("\t%s\t\t%d\t",dir[i].sdname[j],dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)
printf("%s\t",dir[i].fname[j][k]);
printf("\n\t\t");}
printf("\n"); }
getch(); }
OUTPUT:
PROGRAM-20
50
Write a C program for sequential file for processing the student
information.
#include <stdio.h>
#include <conio.h>
#include <string.h>
struct record
{
char empname[20];
int age;
float salary;
};
typedef struct record person;
FILE *people;
void main()
{
person employee;
int i, n;
FILE *fp;
printf("How many records: ");
scanf("%d", &n);
fp = fopen("[Link]", "w");
for (i = 0; i < n; i++)
{
printf("Enter employee information %d (EmpName, Age, Salary): ", i + 1);
scanf("%s%d%f", [Link], &[Link], &[Link]);
fwrite(&employee, sizeof(employee), 1, fp);
}
51
fclose(fp);
int rec, result;
people = fopen("[Link]", "r");
printf("Which record do you want to read from the file? ");
scanf("%d", &rec);
while (rec >= 0)
{
fseek(people, rec * sizeof(employee), SEEK_SET);
result = fread(&employee, sizeof(employee), 1, people);
if (result == 1)
{
printf("\nRECORD %d\n", rec);
printf("Given name: %s\n", [Link]);
printf("Age: %d years\n", [Link]);
printf("Current salary: $%.2f\n\n", [Link]);
}
else
{
printf("\nRECORD %d not found!\n\n", rec);
}
printf("Which record do you want to read (0 to %d): ", n - 1);
scanf("%d", &rec);
}
fclose(people);
getch();
}
OUTPUT:
52
PROGRAM-21
53
Write a C program for random access file for processing the employee
details.
#include<stdio.h>
#include<stdlib.h>
#define MAX_EMPLOYEES 100
#define MAX_NAME_LENGTH 50
struct Employee {
int id;
char name[MAX_NAME_LENGTH];
float salary;
};
void addEmployee(FILE*file,struct Employee*employee)
{
printf("\n This output is produced by Aarish Saifi");
fseek(file,(employee->id-1)*sizeof(struct Employee),SEEK_SET);
fwrite(employee,sizeof(struct Employee),1,file);
printf("Employee added successfully:\n");
}
void displayEmployee(FILE *file,int id){
struct Employee employee;
fseek(file,(id-1)*sizeof(struct Employee),SEEK_SET);
fread(&employee,sizeof(struct Employee),1,file);
printf("\n Employee information:\n");
printf("ID:%d\nName:%s\
nSalary:%.2f\n",[Link],[Link],[Link]);
}
int main()
{
54
FILE*file=fopen("[Link]","wb+");
if(file==NULL){
printf("error opening file:\n");
return 1;
}
struct Employee employees[MAX_EMPLOYEES];
for (int i=0;i<MAX_EMPLOYEES;i++){
employees[i].id=i+1;
sprintf(employees[i].name,"Employee%d",i+1);
employees[i].salary=50000+(i*1000);
}
fwrite(employees,sizeof(struct Employee),MAX_EMPLOYEES,file);
struct Employee newEmployee={101,"John",60000};
addEmployee(file,&newEmployee);
displayEmployee(file,101);
fclose(file);
return 0;
};
OUTPUT:
55
56