****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED THIS MEANS THAT YOU CANT USE fopen, fclose , etc
*****You are ONLY ALLOWED TO USE SYSTEM CALL I/O such as read, write, open and close (System I/O functions)
You will write a C program and the way to use it on command line is as follows:
./myprogram file a t newfile
where both file and newfile are text files, “a” is a single character ( of your choice), and “t” is a single character( of your choice). For example, you could run this program as
./myprogram file b k newfile
What your program does is to create a new file (i.e, newfile) such that the content of newfile is the same as the content of file but every occurrence of the character “a” in file is now replaced by character “t” in newfile.
For example, suppose the content of “file” is
apple bee elephant abort
If I run
./myprogram file a t newfile
Then, the content of the “newfile” should be
tpple bee elephtnt tbort
You could see every occurrence of “a” in file is now “t” in newfile.
If I run
./myprogram file b x newfile
Then, the content of the “newfile” should be
apple xee elephant axort
You could see every occurrence of “b” in file is now “x” in newfile.
Write your program using system i/o functions.
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main ( int argc, char **argv )
{
char infile[32] = "";
char outfile[32] = "";
char inbuf[256] = "";
strcpy(infile, argv[1]);
strcpy(outfile, argv[4]);
char c = argv[2][0];
char r = argv[3][0];
printf(" c= <%c> r = <%c> infile = <%s> outfile = <%s> \n", c, r, infile, outfile );
int fd1 = open( infile, O_RDONLY | O_CREAT);
int fd2 = open( outfile, O_CREAT | O_RDWR , S_IRWXU
);
int size = lseek(fd1, 0, SEEK_END);
lseek(fd1, 0, SEEK_SET);
int sr = read(fd1, inbuf, size);
for(int i = 0; i<= size; i++)
{
if(inbuf[i] == c)
inbuf[i] =
r;
}
if( lseek(fd2, 0, SEEK_SET) == -1)
perror("lseek error");
int sw = write(fd2, inbuf, size);
if (sw == -1)
perror("write error ");
close(fd1); close(fd1);
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.