C Program to show Memory Allocation
#include<fcntl.h>
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
int fd,fp,ch,l=0,q=0;
char *name, *in, *out;
printf("Enter the name of the file:");
scanf("%s",name);
fd=open(name, O_CREAT, S_IRWXU);
if(fd!=-1)
{
printf("%s File is created.\n",name);
close(fd);
while(q==0)
{
printf("Menu\n1.Rdonly\n2.Wronly\n3.Rdwr\n4.Exit\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: fp=open(name,O_RDONLY);
printf("%s File is opened with Read Only mode.\n",name);
l=lseek(fp,0,SEEK_END);
out=(char *) malloc(20);
if(l>0)
{
int fd=open(name,O_RDONLY);
read(fd,out,l);
printf("File Data: %s\n",out);
close(fd);
}
else
printf("File is Empty...\n");
close(fd);
break;
case 2: fd=open(name,O_WRONLY);
printf("%s File is opened with Write Only mode.\n",name);
printf("Enter the Data: \n");
in=(char *) malloc(20);
scanf("%s",in);
write(fd,in,strlen(in));
printf("%s is written to the file %s\n",in,name);
close(fd);
break;
case 3: fd=open(name,O_RDWR);
printf("%s File is opened with Read Write mode.\n",name);
printf("Enter the Data: \n");
scanf("%s",in);
write(fd,in,strlen(in));
l=lseek(fd,0,SEEK_END);
out=(char *) malloc(20);
if(l>0)
{
int fd=open(name,O_RDONLY);
read(fd,out,l);
printf("File Data: %s\n",out);
close(fd);
}
else
printf("File is Empty...\n");
close(fd);
break;
case 4: q=1;
break;
default : printf("Your option is invalid... Try again...\n");
}
}
}
else
printf("Error!! %s already exists...\n",name);
return(0);
}
#include<fcntl.h>
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
int fd,fp,ch,l=0,q=0;
char *name, *in, *out;
printf("Enter the name of the file:");
scanf("%s",name);
fd=open(name, O_CREAT, S_IRWXU);
if(fd!=-1)
{
printf("%s File is created.\n",name);
close(fd);
while(q==0)
{
printf("Menu\n1.Rdonly\n2.Wronly\n3.Rdwr\n4.Exit\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: fp=open(name,O_RDONLY);
printf("%s File is opened with Read Only mode.\n",name);
l=lseek(fp,0,SEEK_END);
out=(char *) malloc(20);
if(l>0)
{
int fd=open(name,O_RDONLY);
read(fd,out,l);
printf("File Data: %s\n",out);
close(fd);
}
else
printf("File is Empty...\n");
close(fd);
break;
case 2: fd=open(name,O_WRONLY);
printf("%s File is opened with Write Only mode.\n",name);
printf("Enter the Data: \n");
in=(char *) malloc(20);
scanf("%s",in);
write(fd,in,strlen(in));
printf("%s is written to the file %s\n",in,name);
close(fd);
break;
case 3: fd=open(name,O_RDWR);
printf("%s File is opened with Read Write mode.\n",name);
printf("Enter the Data: \n");
scanf("%s",in);
write(fd,in,strlen(in));
l=lseek(fd,0,SEEK_END);
out=(char *) malloc(20);
if(l>0)
{
int fd=open(name,O_RDONLY);
read(fd,out,l);
printf("File Data: %s\n",out);
close(fd);
}
else
printf("File is Empty...\n");
close(fd);
break;
case 4: q=1;
break;
default : printf("Your option is invalid... Try again...\n");
}
}
}
else
printf("Error!! %s already exists...\n",name);
return(0);
}
Comments
Post a Comment