Question

My question is listed the below please any help this assignment ; There is a skeleton...

My question is listed the below please any help this assignment ;

There is a skeleton code:  copy_file_01.c

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[]) {
char ch ;
FILE *source , *target;

   if(argc != 3){
printf ("Usage: copy file1 file2");
exit(EXIT_FAILURE);
}

source = fopen(argv[1], "r");

if (source == NULL) {
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}


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

if (target == NULL) {
fclose(source);
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}

while ((ch = fgetc(source)) != EOF)
fputc(ch, target);

printf("File copied successfully.\n");

fclose(source);
fclose(target);

return 0;
}

hw1 using system calls

The following program copies input file *argc[1] to output file *argc[2]. For file access the following C library functions are used:

FILE * fopen ( const char * filename, const char * mode );

int fclose ( FILE * stream );
int fgetc ( FILE * stream );
int fputc ( int character, FILE * stream );

re-write copy_file_01.c program using linux system calls  replacing the functions which are listed above.
this web page  is useful: 
https://www.geeksforgeeks.org/input-output-system-calls-c-create-open-close-read-write/
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

#define BUFF_SIZE 1024

int main(int argc, char* argv[])
{
    int sourceFD,destinationFD,nbread,nbwrite;
    char *buff[BUFF_SIZE];
    
    /*Check if both src & dest files are received */
    if(argc != 3 )
    {
        printf("\nUsage: file1 file2\n");
        exit(EXIT_FAILURE);
    }

    /*Open source file*/
    sourceFD = open(argv[1],O_RDONLY);

    if(sourceFD == -1)
    {
        printf("\nError opening file 1 \n");
        exit(EXIT_FAILURE);    
    }
    
    /*Open destination file with respective flags & modes*/
    destinationFD = open(argv[2],O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);

    if(destinationFD == -1)
    {
        printf("\nError opening file 2   \n");
        exit(EXIT_FAILURE);
    }

    /*Start data transfer from src file to dest file till it reaches EOF*/
    while((nbread = read(sourceFD,buff,BUFF_SIZE)) > 0)
    {
        if(write(destinationFD,buff,nbread) != nbread)
            printf("\nError in writing data to %s\n",argv[2]);
    }
    
    if(nbread == -1)
        printf("\nError in reading data from %s\n",argv[1]);
    
    if(close(sourceFD) == -1)
        printf("\nError in closing file %s\n",argv[1]);

    if(close(destinationFD) == -1)
        printf("\nError in closing file %s\n",argv[2]);

    exit(EXIT_SUCCESS);
}

//ouput

Add a comment
Know the answer?
Add Answer to:
My question is listed the below please any help this assignment ; There is a skeleton...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • Part D. On executing the below program, what will be the output of the following program...

    Part D. On executing the below program, what will be the output of the following program if the source file contains a line "This is a sample."? [5 marks] #include <stdio.h> int main() { char ch; int k, n=0; FILE * fs = fopen("source.txt", "r"); while(1) { ch = fgetc(fs); if(ch == EOF) break; else { for(k=0;k<4;k++) fgetc(fs); printf("%c", ch); n += k; } } printf("%d\n", n); fclose(fs); return 0; } ---------------------------------- Any one can explain the question to me...

  • The program is written in c. How to implement the following code without using printf basically...

    The program is written in c. How to implement the following code without using printf basically without stdio library? You are NOT allowed to use any functions available in <stdio.h> . This means you cannot use printf() to produce output. (For example: to print output in the terminal, you will need to write to standard output directly, using appropriate file system calls.) 1. Opens a file named logfle.txt in the current working directory. 2. Outputs (to standard output usually the...

  • Writing a program in C please help!! My file worked fine before but when I put...

    Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...

  • need this in c programming and you can edit the code below. also give me screenshot...

    need this in c programming and you can edit the code below. also give me screenshot of the output #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define LINE_SIZE 1024 void my_error(char *s) { fprintf(stderr, "Error: %s\n", s); perror("errno"); exit(-1); } // This funciton prints lines (in a file) that contain string s. // assume all lines has at most (LINE_SIZE - 2) ASCII characters. // // Functions that may be called in this function: // fopen(), fclose(), fgets(), fputs(),...

  • Below is a basic implementation of the Linux command "cat". This command is used to print...

    Below is a basic implementation of the Linux command "cat". This command is used to print the contents of a file on the console/terminal window. #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) {FILE *fp; if(2 != argc) {priritf ("Usage: cat <filename>\n"); exit(1);} if ((fp = fopen(argv[1], "r")) == NULL) {fprintf (stderr, "Can't. open input file %s\n", argv[1]); exit (1);} char buffer[256]; while (fgets(X, 256, fp) != NULL) fprintf(Y, "%s", buffer); fclose(Z); return 0;} Which one of the following...

  • Modify the client server system program given below so that instead of sendto() and recvfrom(), you...

    Modify the client server system program given below so that instead of sendto() and recvfrom(), you use connect() and un-addresssed write() and read() calls. //Server.c #include #include #include #include #include #include #include #include #include #include # define PortNo 4567 # define BUFFER 1024 int main(int argc, char ** argv) { int ssd; int n; socklen_t len; char msg[BUFFER]; char clientmsg[BUFFER]; struct sockaddr_in server; struct sockaddr_in client; int max_iterations = 0; int count = 0, totalChar = 0, i = 0;...

  • Question: For the picture writing question, if the question says that the picture length (height) and...

    Question: For the picture writing question, if the question says that the picture length (height) and width are multiples of 5, then be prepared (for example) to handle a situation where you are being asked to blacken the fourth (vertical) strip from the left. Code: #include #include #include #include #define BUFFER_SIZE 70 #define TRUE 1 #define FALSE 0 int** img; int numRows; int numCols; int maxVal; FILE* fo1; void addtopixels(int** imgtemp, int value); void writeoutpic(char* fileName, int** imgtemp); int** readpic(char*...

  • C Language Programming. Using the program below - When executing on the command line only this...

    C Language Programming. Using the program below - When executing on the command line only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...

  • Write a complete C program that inputs a paragraph of text and prints out each unique...

    Write a complete C program that inputs a paragraph of text and prints out each unique letter found in the text along with the number of times it occurred. A sample run of the program is given below. You should input your text from a data file specified on the command line. Your output should be formatted and presented exactly like the sample run (i.e. alphabetized with the exact spacings and output labels). The name of your data file along...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT