Question

In: Computer Science

This code is an expression of cp command in c language. But I don't understand this...

This code is an expression of cp command in c language. But I don't understand this code very well. Please explain in notes one by one.

#define SIZE 1024

#include<string.h>
#include<stdio.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/stat.h>


int main(int argc, char *argv[]){

if(argc != 3){
perror("argument 부족\n");
exit(0);
}

struct stat frstatbuf;
FILE* fr = fopen(argv[1], "r");

if(fr == NULL){
perror("read file 읽기 오류\n");
exit(0);
}

int frfd = fileno(fr);

fstat(frfd, &frstatbuf);

FILE* fw=fopen(argv[2], "w+");

int fwfd=fileno(fw);

fchmod(fwfd,frstatbuf.st_mode&(S_IRWXU|S_IRWXG|S_IRWXO));

char buf[1024];

while(1){
int n=fread(buf,sizeof(char),SIZE,fr);

if(n<SIZE){
fwrite(buf,sizeof(char),n,fw);

printf("파일을 다 읽었음\n");
exit(0);
}
fwrite(buf,sizeof(char),n,fw);
}

fclose(fr);
fclose(fw);
return 0;
}

Solutions

Expert Solution

#define SIZE 1024

#include<string.h>
#include<stdio.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/stat.h>


int main(int argc, char *argv[]) {
    
    // validate command line arguments.
    if(argc != 3){
        perror("argument 부족\n");
        exit(0);
    }

    struct stat frstatbuf;
    
    // Open the file and get a file pointer
    FILE* fr = fopen(argv[1], "r");
    if(fr == NULL){
        perror("read file 읽기 오류\n");
        exit(0);
    }
    
    // Get file descriptor from file pointer
    int frfd = fileno(fr);
    
    // Read file related information into file status buffer: frstatbuf
    fstat(frfd, &frstatbuf);
    
    // Open a new file for writing
    FILE* fw=fopen(argv[2], "w+");
    
    // get file descriptor for file to write
    int fwfd=fileno(fw);
    
    // Change the file permissio of writing file
    // Give read write execute permission to group, owner and others.
    fchmod(fwfd, frstatbuf.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO));
    
    // buffer to read file contents.
    char buf[1024];

    while(1){
        
        // try reading SIZE bytes maximum from file with descriptor fr
        // (from input file)
        // The return value is the number of bytes read successfully
        int n=fread(buf,sizeof(char),SIZE,fr);
        
        // If we are unable to read all the required number of bytes
        // then it is a errror
        if(n < SIZE){
            fwrite(buf,sizeof(char),n,fw);

            printf("파일을 다 읽었음\n");
            
            // end the program here after printing the error.
            exit(0);
        }
        
        // Else, everything was fine, we were able to read the data from file
        // Now, write the same number of bytes to output file with descriptor fw.
        fwrite(buf,sizeof(char),n,fw);
    }
    
    // at last close all the resources
    fclose(fr);
    fclose(fw);
    return 0;
}

**************************************************

please see inline comments.

I have tried to answer your question to best of my efforts. However, if you still face any issues in the answer, please let me know via the comment section. Your feedback will imporve as well as encourage me to keep up the good work.

If i was able to help you, then please provide me an upvote(thumbs up). Thanks!


Related Solutions

This code is an expression of cp command in c language. But I don't understand this...
This code is an expression of cp command in c language. But I don't understand this code very well. Please explain in notes one by one.(in detail) #include<stdio.h> #include<stdlib.h> #include<fcntl.h> #include<errno.h> #include<stdlib.h> #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> int main(int argc, char *argv[]) { char ch; int src, dst; struct stat st; if(argc != 3) { printf("argument error \n"); printf("usage: ./a.out src dest \n"); exit(0); } if (stat(argv[1], &st) == -1) { perror("stat : "); exit(-1); } src = open(argv[1], O_RDONLY); if(src...
I made the command cp code in c language. But when I copy a file, file...
I made the command cp code in c language. But when I copy a file, file permissions are not copied equally. So I want to copy the file authority as well. What should I do? #include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { struct stat st; char ch; int src, dst; if(argc != 3) { printf("argument error \n"); printf("usage: ./a.out src dest \n"); exit(0); } src = open(argv[1], O_RDONLY); if(src == -1){ perror("open...
I don't understand the Van't Hoff factor.. I feel dumb because I don't understand it
I don't understand the Van't Hoff factor.. I feel dumb because I don't understand it
What is the advantage of ln command over cp command?
What is the advantage of ln command over cp command?
PHP Language I have XAMPP and I don't know how to display my PHP code. For...
PHP Language I have XAMPP and I don't know how to display my PHP code. For instance, when I create a basic HTML webpage I just open the code into a web browser and it shows me a basic web page. When I try to show a PHP calculation it doesn't show. What are the steps in displaying my PHP doc?
please I don't understand this code. Can you put comments to explain the statements. Also, if...
please I don't understand this code. Can you put comments to explain the statements. Also, if there any way to rewrite this code to make it easier, that gonna help me a lot. import java.io.*; import java.util.*; public class State {    private int citi1x,citi1y; private int pop1; private int citi2x,citi2y; private int pop2; private int citi3x,citi3y; private int pop3; private int citi4x,citi4y; private int pop4; private int plantx,planty; public int getCity1X(){ return citi1x; } public int getCity1Y(){ return citi1y;...
I don't understand how the bonds chart work.
I don't understand how the bonds chart work.
HI, I hope you are doing well. I really don't understand this question and don't know...
HI, I hope you are doing well. I really don't understand this question and don't know how to solve it at all because I am completely new to this c++ programming. can you please explain each line of code with long and clear comments? please think of me as someone who doesn't know to code at all. and I want this code to be written in c++ thank you very much and I will make sure to leave thumbs up....
This is my C language code. I have some problems with the linked list. I can't...
This is my C language code. I have some problems with the linked list. I can't store the current. After current = temp, I don't know how to move to the next node. current = current-> next keeps making current into NULL. #include #include #include #include struct node{int data; struct node *next;}; int main() {     struct node *head, *current, *temp, *trash;     srand(time(0));     int randNumber = rand()%51;     if(randNumber != 49)     {         temp = (struct node*)malloc(sizeof(struct node));         current = (struct node*)malloc(sizeof(struct node));...
I have written code in C programming that checks where the command line arguments are floats...
I have written code in C programming that checks where the command line arguments are floats or not. For example, if I type "./math 1 1 0 0 2.5 3" in the terminal, my program realizes they are all floats but if I type "./math 1 1 0 0 2.5 g", it recognizes that not all arguments are floats and gives an error message. I want to take my code further such that after typing in "./math 1 1 0...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT