// Filename: cpfile.c // Compile command: gcc cpfile.c -o cpfile.exe #include #include #include #define SIZ 128L // 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"); // exit(1); return 1; } // Can we access thge source file? if ((f1 = open(argv[1], O_RDONLY, 0)) printf("Can't open %s \n", argv[1]); return 2; -1){ == } // Can we create the target file? if ((f2 = creat(argv[2], 0644)) == printf("Can't create %s \n", argv[2]); return 3; -1){ %3D } // 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;

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter8: I/o Streams And Data Files
Section: Chapter Questions
Problem 8PP: (Data processing) A bank’s customer records are to be stored in a file and read into a set of arrays...
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>
#define SIZ 128L
// 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");
// exit(1);
return 1;
}
// Can we access thge source file?
if ((f1 = open(argv[1], O_RDONLY, 0))
printf("Can't open %s \n", argv[1]);
return 2;
-1){
==
// 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;
Transcribed Image Text:// Filename: cpfile.c // Compile command: gcc cpfile.c -o cpfile.exe #include <stdio.h> #include <fcntl.h> #include <unistd.h> #define SIZ 128L // 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"); // exit(1); return 1; } // Can we access thge source file? if ((f1 = open(argv[1], O_RDONLY, 0)) printf("Can't open %s \n", argv[1]); return 2; -1){ == // 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;
Expert Solution
Step 1

Given C Program

#include<stdio.h> 
#include<string.h> 
#include<unistd.h> 
#include<fcntl.h> 
#define SIZ 128
int main (int argc,const char * argv[]) 

    int f1,f2;
    if(argc!=3)
    {
        printf("Wrong number of command line arguments\n");
        // exit(1);
        return 1;
    }    
  if((f1=open(argv[1],O_RDONLY,0))==-1)
  {
      printf("Cant open %s\n",argv[1]);
      return 2;
  }
  if((f2=creat(argv[2],0644))==-1)
  {
  printf("Cant create %s\n",argv[2]);
      return 3;
  }
  char buf[SIZ];
  int n;
  while((n=read(f1,buf,SIZ))>0)
      if(write(f2,buf,n)!=n)
      {
          printf("cant write file");
          close(f1);
           close(f2);
           return 4;
      }
  
 

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