Question

In: Computer Science

Rewrite the attached mycat.c program in 3 using read, write, open and close (System I/O functions)...

Rewrite the attached mycat.c program in 3 using read, write, open and close (System I/O functions) instead of the standard I/O functions.

#include<stdio.h>


int main(int argc, char* argv[])
{
        FILE *fp;

        void filecopy(FILE *, FILE *);

        if (argc == 1)
        {
                filecopy(stdin, stdout);
        }
        else
        {
                while(--argc >0)
                {
                        if ((fp = fopen(*++argv, "r")) == NULL)
                        {
                                printf("cat: can not open %s\n", *argv);
                                return 1;
                        }
                        else
                        {
                                filecopy(fp, stdout);
                                fclose(fp);
                        }
                }
        }

        return 0;
}

void filecopy(FILE *ifp, FILE *ofp)
{
        int c;

        while ((c = getc(ifp)) != EOF)
        {
                putc(c, ofp);
        }
}

Solutions

Expert Solution

Se here is the changed prohram.

#include<stdio.h>
#include<fcntl.h>
//we need to import fcntl to give the modes of opening the files like O_RDONLY
#include<stdlib.h>
#include<string.h>

//Main driver function
int main(int argc, char *argv[])
{
int fd;
//here in system calls we will definee the file descriptor fd
// here fd has three paramets 0- input,1- output 2- standard error
char c;
if(argc==3)
{
//open the first file in the read only mode and check if file is
// opened successfully or not if not exit from the program
fd = open(argv[1], O_RDONLY, 0);
//it is unable to open the file it will return -1
if((fd)==-1){
//Failed to open it will return the fd as -1
printf("\nError opening the file") ;
exit(1);
}
char buf1[100];
int idx = 0;
while(read(fd,&c,1)!=0){
//priint data read will return the number of items read
//(read takes 3 parameters 1->filedescriptor,2-> buffer to store data,
// 3->number of items to be read)
printf("%c",c);
buf1[idx++] = c;
}
buf1[idx++] = '\0';
int fdw;
fdw = open(argv[2], O_CREAT); // create file if not present
close(fdw);
fdw = open(argv[2], O_WRONLY); // open file in write only mode
//write data to the file(write takes 3 parameters
write(fdw, buf1, strlen(buf1));
close(fdw);
}
//close the file
close(fd);
}

Run the compiled code with two command line arguments a.txt and b.txt

The context of a.txt will be copied in b.txt.


Related Solutions

Rewrite the attached code program using read, write, open and close (System I/O functions) instead of...
Rewrite the attached code program using read, write, open and close (System I/O functions) instead of the standard I/O functions. Also please write comments explaining each line of code clearly and concisely please. #include #include int main(int argc, char *argv[]) { FILE *fd;    char c;    if(argc==1)    fd=stdin;    else         if((fd = fopen(argv[1], "r"))==NULL){        fprintf(stderr, "Error opening %s, exiting\n", argv[1]);            exit(0);    }    while( (c=getc(fd)) != EOF)    putc(c, stdout);   ...
Write a C program using system call I/O to a) open an existing text file passed...
Write a C program using system call I/O to a) open an existing text file passed to your program as a command line argument, then b) display the content of the file, c) ask the user what information he/she wants to append d) receive the info from the user via keyboard e) append the info received in d) to the end of the file f) display the updated content of the file
Write a C program using system call I/O to ****NOTE YOU MUST USE SYSTEM CALL I/O,...
Write a C program using system call I/O to ****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED a) open an existing text file passed to your program as a command-line argument, then b) display the content of the file, c) ask the user what information he/she wants to append d) receive the info from the user via keyboard e) append the info received in d) to the end of the file f) display the updated...
Python - Rewriting a Program Rewrite Program 1 using functions. The required functions are in the...
Python - Rewriting a Program Rewrite Program 1 using functions. The required functions are in the table below. Create a Python program that will calculate the user’s net pay based on the tax bracket he/she is in. Your program will prompt the user for their first name, last name, their monthly gross pay, and the number of dependents. The number of dependents will determine which tax bracket the user ends up in. The tax bracket is as follows: 0 –...
Write a C program, called reverse, using standard I/O functions, to take a file as input...
Write a C program, called reverse, using standard I/O functions, to take a file as input then copies it to another file in reverse order. That is, the last byte becomes the first, the byte just before the last one becomes the second, etc. The program call should look like: reverse fileIn fileOut
7) Write a program to open an input dialog box and read a string value. Write...
7) Write a program to open an input dialog box and read a string value. Write the string back to the user using a message box. USING MIPS ASSEMBLY LANGUAGE
Using python Write a program that has 3 functions, named write_to_file, read_from_file, and ask_user. The write_to_file...
Using python Write a program that has 3 functions, named write_to_file, read_from_file, and ask_user. The write_to_file function should have 2 parameters, file_name and data. When called, the function will open a file with the name stored in the file_name variable, write the information stored in data, then close the file. The read_from_file function will have 1 parameter, file_name. When called, the function will open a file with the name stored in the file_name variable, print the contents of the file,...
****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED Write a C...
****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED Write a C program using system call I/O to determine how many lines there are in a text file.
Problem Write a movie management system using object-oriented design principles. The program will read from the...
Problem Write a movie management system using object-oriented design principles. The program will read from the supplied data file into a single array list. The data file (movies.txt) contains information about the movies. Each movie record has the following attributes: - Duration (in minutes) - Title - Year of release Each record in the movies.txt file is formatted as follows: - Duration,Title,Year - e.g.: 91,Gravity,2013 Specifically, you have to create an interactive menu driven application that gives the user the...
write C++ program using functions (separate function for each bottom) Write a program to find if...
write C++ program using functions (separate function for each bottom) Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. . the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT