Question

In: Computer Science

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);

   exit(0);
}

Solutions

Expert Solution

SOLUTION

#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>
//string.h to have the string methods
int main(int argc, char *argv[])
{
int fd;
//here in system calls we will defimee the file descriptor fd unilike the File pointer in the
// here fd has three paramets 0- input,1- output 2- standard error
char c;
if(argc==2)
{
   //open the first file in the read, write mode and check if file is opened successfully or not if not exit from the program
       fd = open(argv[1], O_RDWR);
       //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);
        }
        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);
       }
       //write data to the file(write takes 3 paramets same as read )
        write(fd,"Hello",strlen("HelloS"));
       while(read(fd,&c,1)!=0){
            printf("%c",c);
       }
   }
   //close the file
close(fd);
}

OUTPUT

This is the output after running the program two times so it has two hello inserts

FIle:


Related Solutions

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
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting...
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting clc, clear, format compact; %define variables k=1; b=-2; x=-1; y=-2; %while loop initialization for k <= 3 disp([num2str(k), ' ',num2str(b),' ',num2str(x),' ',num2str(y),]); y = x^2 -3; if y< b b = y; end x = x+1; k = k+1; end
Matlab code problems I have to write a code using functions to find out if a...
Matlab code problems I have to write a code using functions to find out if a year is a leap year. I'm on the write track i feel like but I keep getting an error message and matlab isnt helping to troubleshoot. The issue is on line 30. Here is my code: function project_7_mfp() %   PROJECT_7_ABC   project_7_abc() creates a function that prompts the user %   enter a year after 1582 It then determines if it is a leap year %...
I am Writing a C-Program to read and write files. but none of my code is...
I am Writing a C-Program to read and write files. but none of my code is working like it should be. Please fix all code and supply output response. Please try to use existing code and code in comments. But if needed change any code that needs to be changed. Thank you in advance //agelink.c //maintains list of agents //uses linked list #include <stdio.h> #include <stdlib.h> #define TRUE 1 void listall(void); void newname(void); void rfile(void); void wfile(void); struct personnel {...
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
For these of string functions, write the code for it in C++ or Python (without using...
For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type. 1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str
PLEASE WRITE IN C++ PROGRAM THANKS - QUEUES Please study the code posted below. Please rewrite...
PLEASE WRITE IN C++ PROGRAM THANKS - QUEUES Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max capacity using namespace std; /** Queue structure definition */ struct QueueType { int data; struct...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT