Question

In: Computer Science

Modify the bellow program by using only system calls to perform the task. These system calls...

Modify the bellow program by using only system calls to perform the task. These

system calls are open,read,write, and exit .Use text and binary files to test your programs.

#include <stdio.h>

void main(int argc,char **argv)

{

FILE *fp1, *fp2;

char ch, buf[20], *pos;

int n;

pos = buf;

if ((fp1 = fopen(argv[1],"r")) == NULL)

{

puts("File cannot be opened");

exit(1);

}

fp2 = fopen(argv[2], "w");

while ((n = fread(pos, sizeof(char), 20, fp1)) == 20)

{

fwrite(pos, sizeof(char), 20, fp2);

}

fwrite(pos, sizeof(char), n, fp2);

fclose(fp1);

fclose(fp2);

exit(0);

}

Solutions

Expert Solution

Following is the program

#include <stdio.h>
#include<fcntl.h>
#include<errno.h>

void main(int argc,char **argv)

{

//FILE *fp1, *fp2; no longer needed

char ch, buf[20], *pos;

int n;

pos = buf;

//created file descripter for files
int fp1, fp2;

//fp1 = fopen(argv[1],"r") changed to
if ((fp1 = open(argv[1], O_RDONLY)) == -1)

{

puts("File cannot be opened");

exit(1);

}

//fp2 = fopen(argv[2], "w"); changed to
fp2 = open(argv[2], O_WRONLY);

//n = fread(pos, sizeof(char), 20, fp1) changed to
while ((n = read (fp1, pos, 20)) == 20)

{

//fwrite(pos, sizeof(char), 20, fp2); changed to
write (fp2, pos, 20);

}

//fwrite(pos, sizeof(char), n, fp2);changed to
write (fp2, pos, n);

//fclose(fp1); changed to
close(fp1);

//fclose(fp2); changed to
close(fp2);

exit(0);

}

------------------------------------------

In Short, new syntax is this and it is no longer required to enter Size of Byte parameter,

int open (const char* Path, int flags [, int mode ]);
int close(int fd); 
size_t read (int fd, void* buf, size_t cnt);  
size_t write (int fd, void* buf, size_t cnt); 

as for detailed study of system calls regarding open write read and close you can use this reference:

https://www.geeksforgeeks.org/input-output-system-calls-c-create-open-close-read-write/


Related Solutions

Write a program fragment (not a complete program) which will perform the following task: The int...
Write a program fragment (not a complete program) which will perform the following task: The int variable m currently contains the number of minutes a basketball player played in their last game. Use an IF statement(s) to print out an appropriate message based on the following: If m is from 35 to 48, print the message "very tired" If m is from 10 to 34, print the message "little tired" If m is from 1 to 9, print the message...
Please design a PLC program to perform the following task: An LED will be on when...
Please design a PLC program to perform the following task: An LED will be on when it’s activated by an NO push button for an accumulated 6 seconds. In other words, the push button can be on and off, but when it’s accumulated for six seconds, the LED will be on. After six seconds, the LED will be on for four seconds and is then reset itself for another cycle. Post LogixPro image of this programming Cascading timer Assume the...
You will write a Java Application program to perform the task of generating a calendar for...
You will write a Java Application program to perform the task of generating a calendar for the year 2020. You are required to modularize your code, i.e. break your code into different modules for different tasks in the calendar and use method calls to execute the different modules in your program. Your required to use arrays, ArrayList, methods, classes, inheritance, control structures like "if else", switch, compound expressions, etc. where applicable in your program. Your program should be interactive and...
Your task is to modify the program from the Java Arrays programming assignment to use text...
Your task is to modify the program from the Java Arrays programming assignment to use text files for input and output. I suggest you save acopy of the original before modifying the software. Your modified program should: contain a for loop to read the five test score into the array from a text data file. You will need to create and save a data file for the program to use. It should have one test score on each line of...
Give four reasons why system calls are typically implemented using interrupts instead of function calls.
Give four reasons why system calls are typically implemented using interrupts instead of function calls.
1. Write a Java program and Submit only "one .java file" calledNumbers that calls the...
1. Write a Java program and Submit only "one .java file" called Numbers that calls the following methods and displays the returned value:o Write a method called cubeIt that accepts one integer parameter and returns the value raised to the third power as an integer.2. Write a method called randomInRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive.o Write a method called larger that accepts two double parameters and...
Task: there are mainly five types of system calls: Process Control, File Management, Device Management, Information...
Task: there are mainly five types of system calls: Process Control, File Management, Device Management, Information Maintenance and Communication. Please write two python programs to implement the above two types of system calls.
Make a program to perform Heap Sort, must run in Alice programming only. Only correct answers...
Make a program to perform Heap Sort, must run in Alice programming only. Only correct answers needed should be in given language Wrong answers will be downvoted
What are System calls. Provide 6 file and directory system calls
What are System calls. Provide 6 file and directory system calls
assume a program in unix that have a multiple system calls how to show the list...
assume a program in unix that have a multiple system calls how to show the list of system calls used in the program?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT