Rewrite the following C program using only system calls such as open, close, read, write, clear, loctl, etc. Don't use calls from C or C++ libraries uncluding cout, cin, printf, scanf, etc.

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter8: I/o Streams And Data Files
Section8.2: Reading And Writing Character-based Files
Problem 6E: (Data processing) a. Write a C++ program that opens a file and displays its contents with line...
icon
Related questions
Question

Rewrite the following C program using only system calls such as open, close, read, write, clear, loctl, etc. Don't use calls from C or C++ libraries uncluding cout, cin, printf, scanf, etc.

// Filename: cpfile.c
// Compile command: gcc cpfile.c -o cpfile.exe
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
zude kunista
#define SIZ 128
// Copy from source file to target file. Adapted from The C Programming Language
// by Kernighan and Ritchie.
int main(int argc, const char *argv[]) {
int f1, f2;
// Do we have right number of arguments?
if (argc != 3){
printf("Wrong number of command line arguments\n");
7 exit(1);
return 1;
// Can we access thge source file?
if ((f1 = open(argv[1], O_RDONLY, 0)) == -1){
printf("Can't open %s \n", argv[1]);
return 2;
}L
// Can we create the target file?
if ((f2 = creat(argv[2], 0644)) == -1){
printf("Can't create %s \n", argv[2]);
return 3;
// Copy source file contents to target file.
char buf[SIZ];
int n;
while ((n = read( f1, buf, SIZ)) > 0)
if (write(f2, buf, n) != n)
printf("Can 't write file" );
close(f1);
close(f2);
return 4;
close(f1);
close(f2);
write("Success!" );
return 0;
}
Transcribed Image Text:// Filename: cpfile.c // Compile command: gcc cpfile.c -o cpfile.exe #include <stdio.h> #include <fcntl.h> #include <unistd.h> zude kunista #define SIZ 128 // Copy from source file to target file. Adapted from The C Programming Language // by Kernighan and Ritchie. int main(int argc, const char *argv[]) { int f1, f2; // Do we have right number of arguments? if (argc != 3){ printf("Wrong number of command line arguments\n"); 7 exit(1); return 1; // Can we access thge source file? if ((f1 = open(argv[1], O_RDONLY, 0)) == -1){ printf("Can't open %s \n", argv[1]); return 2; }L // Can we create the target file? if ((f2 = creat(argv[2], 0644)) == -1){ printf("Can't create %s \n", argv[2]); return 3; // Copy source file contents to target file. char buf[SIZ]; int n; while ((n = read( f1, buf, SIZ)) > 0) if (write(f2, buf, n) != n) printf("Can 't write file" ); close(f1); close(f2); return 4; close(f1); close(f2); write("Success!" ); return 0; }
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
ADT and Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,