C Programming
C Programming
Lab #07
SUBMITTED TO:
SUBMITTED BY:
ROLL NUMBER:
19-SE-31
SECTION:
Alpha (α)
DATE:
24-11-2021
Lab Task
Task 1:
Write “read” and “write” commands for the following conditions
Read data from standard input and write it on standard output.
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<unistd.h>
#include<strings.h>
#include<limits.h>
int main()
{
char buff[200];
read(STDIN_FILENO,buff,100);
write(STDOUT_FILENO,buff,100);
printf("\n");
return 0;
}
Read data from the file, given as an argument by the user and write it on standard
output.
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<unistd.h>
#include<strings.h>
#include<limits.h>
int main()
{
char buff[200];
char fileName[40];
int fd;
printf("\n Enter the FileName: ");
scanf("%s",fileName);
fd = open(fileName,O_RDWR);
if(fd != -1)
{
read(fd,buff,200);
printf("\n");
write(STDOUT_FILENO,buff,200);
printf("\n");
close(fd);
}
else
{
printf("\n %s Not Found!\n",fileName);
}
return 0;}
Read data from standard input and write it on the file given as an argument by the
user.
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<unistd.h>
#include<strings.h>
#include<limits.h>
int main()
{
char buff[200];
char fileName[40];
int fd;
read(STDIN_FILENO,buff,100);
printf("\n Enter the FileName: ");
scanf("%s",fileName);
fd = open(fileName,O_RDWR);
if(fd != -1)
{
write(fd,buff,200);
printf("\n Data has been Written Into the %s.", fileName );
printf("\n");
close(fd);
}
else
{
printf("\n %s Not Found! \n",fileName);
}
return 0;
}
Task 2:
Examine the program, then translate and execute it.
Copy the contents of an input file to an output file using system calls.
#include<stdlib.h>
#include<unistd.h>
#include<strings.h>
#include<limits.h>
#include<fcntl.h>
#include<sys/stat.h>
#define BUFFER_SIZE 64
int main()
{
char buffer[BUFFER_SIZE];
int in_fd;
int in_bytes;
int out_fd;
in_fd = open(“lab3.txt”,O_RDONLY);
out_fd = open(“lab4.txt”,O_RDWR|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
for(;;)
{
in_bytes = read(in_fd,buffer,BUFFER_SIZE);
if(in_bytes==0)break;
write(out_fd,buffer,in_bytes);
}
close(in_fd);
close(out_fd);
exit(0);
return 0;
}
i. What is the effect of executing this program?
The contents of lab3.txt will be copied to lab4.txt. If lab4.txt does not exist then it will be
created automatically.
ii. How end of files is detected by program?
In this program the end of files is detected by the in_bytes variable. When number of
bytes read from lab3.txt will be zero then that means EOF.
iii. Describe the arguments used in the first call to “open”.
in_fd = open(“lab3.txt”,O_RDONLY);
In this program the first argument is the name of the file that is to be opened. The second
argument is showing the access which has been given on opening the file.
iv. Describe the arguments used in second call to “open”.
out_fd = open(“lab4.txt”,O_RDWR|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
In this program the first argument is the name of the file that is to be opened. In second
argument O_RDWR is showing that the read and write access has been given on opening the
file. O_CREAT will open the file with creating the file. O_TRUNC will open the file with
truncation. In third argument S_IRUSR read permission bit for the owner of the file. S_IWUSR
write permission bit for the owner of the file.
Assignment 3
Write complete code to copy the contents of an input file to an output file using system
calls where the input and output files names are entered by the user.
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<unistd.h>
#include<strings.h>
#include<limits.h>
int main()
{
char buff[200];
char in_fileName[40];
char out_fileName[40];
int in_fd,out_fd,in_bytes;
printf("\n Enter the FileName (read): ");
scanf("%s",in_fileName);
printf("\n Enter the FileName (write): ");
scanf("%s",out_fileName);
in_fd = open(in_fileName,O_RDONLY);
out_fd = open(out_fileName,O_RDWR|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
if(in_fd != -1)
{
for(;;)
{
in_bytes = read(in_fd,buff,200);
if(in_bytes==0)break;
write(out_fd,buff,in_bytes);
}
printf("\n Data has been Written into %s successfully.\n", out_fileName );
close(in_fd);
close(out_fd);
exit(0);
}
else
{
printf("\n %s Not Found! \n",in_fileName);
}
return 0;
}
Write complete code to read the content from standard input to a file and then write the
content on standard output.
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<unistd.h>
#include<strings.h>
#include<limits.h>
int main()
{
char buff[200];
char fileName[40];
int fd,in_bytes;
read(STDIN_FILENO,buff,200);
printf("\n Enter the FileName (write): ");
scanf("%s",fileName);
fd = open(fileName,O_RDWR|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
write(fd,buff,200);
printf("\n Data has been Written into %s successfully.\n",fileName );
read(fd,buff,250);
printf("\n");
write(STDOUT_FILENO,buff,200);
close(fd);
exit(0);
return 0;
}