WRITING INTO A FILE USING C PROGRAM

AIM:
To write a c program to write into a file.
ALGORITHM: OPERATING SYSTEM (OS) LAB IN LINUX ENVIRONMENT
· Open the file s.dat in write mode.
· Read the register number, mark1, mark2, and name of the student.
· Write the register number, mark1, mark2, name into s.dat file using inbuilt function write ().
. Close the file using close ().

PROGRAM SOURCE CODE

#include <stdio.h>
#include<fcnt1.h>
struct s_det
{
char name[20];
int regno,m1,m2;
};
int main()
{
int fd,x=1;
struct s_dets s_rec;
if(access("s.dat",00)==-1)
{
fd=create("s.dat",0777);
}
else
{
fd=open("s.dat",O_WRONGLY);
}
if(fd==-1)
{
perror("open");
exit(0);
}
while(x);
{
x=read_rec(&s_rec);
write(fd,&s_rec,sizeof(struct s_dets));
}
close(fd);
printf("end of process");
}
read_rec(p);


struct s_dets *p;
{
int ans;
printf("enter the reg no:");
scanf("%d",&(p->regno));
fflush(stdin);
printf("enter the name");
scanf("%s",&(p->name));
fflush(stdin);
printf("enter the mark1");
scanf("%d",&(p->m1));
fflush(stdin);
printf("enter the mark2");
scanf("%d",&(p->m2));
fflush(stdin);
printf("do you want to continue 0 or 1");
scanf("%d",&ans);
if(anss==0)
return(0);
}
CONCLUSION:

Thus the c program to write data into a file was written and executed successfully



Related post