Question

In: Computer Science

Main task is solving problem, in this code there is problem. and you should explain? #include...

Main task is solving problem, in this code there is problem. and you should explain?
#include <unistd.h> 
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[]){ 
 int fd1, fd2;
 char buffer[100];  long int n1;

 if(((fd1 = open(argv[1], O_RDONLY)) == -1) ||  ((fd2 = open(argv[2],  
       O_CREAT|O_WRONLY|O_TRUNC,0700)) == -1)){
     perror("file problem ");  
     exit(1);
 }

 while((n1=read(fd1, buffer, 512) > 0))  
      if(write(fd2, buffer, n1) != n1){
             perror("writing problem ");  exit(3);
       }

// Case of an error exit from the loop  
 if(n1 == -1){
    perror("Reading problem ");  
    exit(2);
  }
  close(fd2);  
  exit(0);
}

Solutions

Expert Solution

We need to just read byte by byte from the file i.e. instead of 512 bytes we need to read 1 byte at a time. I just changed the line

while((n1=read(fd1, buffer, 512) > 0))  
to 
while((n1=read(fd1, buffer, 1) > 0))  

now my contents in 1st file are getiing copied and written to 2nd file.

I'm attaching my code below:

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

int main(int argc, char *argv[]){ 
 int fd1, fd2;
 char buffer[100];  long int n1;
 fd1= open(argv[1], O_RDONLY);
 fd2 = open(argv[2],O_CREAT|O_WRONLY|O_TRUNC,0700);

 if(fd1==-1 ||  fd2 == -1){
     perror("file problem ");  
     exit(1);
 }

 //char buffer[20];
 size_t nbytes;
 ssize_t bytes_read;

 while((n1=read(fd1, buffer, 1) > 0))  
      if(write(fd2, buffer, n1) != n1){
             perror("writing problem ");  exit(3);
       }

// Case of an error exit from the loop  
 if(n1 == -1){
    perror("Reading problem ");  
    exit(2);
  }
  close(fd2);  
  exit(0);
}

Related Solutions

construct A*star algorithm for solving the 8-puzzle problem . Use MATLAB or Python .Your code should...
construct A*star algorithm for solving the 8-puzzle problem . Use MATLAB or Python .Your code should include two heuristic functions -misplaced tiles and calculation of manhattan distance. The code should work for all cases of puzzle.
For this problem, you will be asked to write some code to accomplish a particular task...
For this problem, you will be asked to write some code to accomplish a particular task given the code fragment below. Each task may depend on the tasks that came before it. Your code must be syntactically correct. None of your operations for this problem may affect S::m_unique. class S { public: S(int init, int size, int id) :m_num(init), m_size(size) {                m_unique = new int[m_num]; for (int i = 0; i < m_num; i++)       m_unique[i] = id;...
Task: Apply mathematical problem solving skills to a variety of problems at the college level. To...
Task: Apply mathematical problem solving skills to a variety of problems at the college level. To accomplish this task, the students will 1. Identify what they are given and what they need to find; 2. Identify the type of problem they have been given and the tools necessary to solve the problem; 3. Correctly apply the tools to the information given to set up the problem; 4. Perform mathematically correct calculations to determine a solution; 5. Interpret their results in...
What is Group Problem Solving? Explain the different types of problem-solving techniques. Please explain what technique...
What is Group Problem Solving? Explain the different types of problem-solving techniques. Please explain what technique you think is most effective and why?
C programming #include <stdio.h> #include <math.h> int main() { printf("============== Problem #1 =================\n"); printf("This should print...
C programming #include <stdio.h> #include <math.h> int main() { printf("============== Problem #1 =================\n"); printf("This should print down from 2 to 0 by 0.1 increments\n"); float f = 2.0; while (f != 0) { printf("%0.1f\n",f); f = f - 0.1; } printf("============== Problem #2 =================\n"); printf("This should find that the average is 5.5\n"); int total_score = 55; int total_grades = 10; double avg = total_score/total_grades; printf("Average: %0.2f\n",avg); printf("============== Problem #3 =================\n"); printf("If the population increases by 2.5 people per second, how...
here it is running nonstop. so here one thing should be wrong. your task is solving...
here it is running nonstop. so here one thing should be wrong. your task is solving that problem #include <stdio.h> #include<unistd.h> int main(int argc, char *argv[]){ FILE *fd; unsigned char ch; //what will happen if you uncomment this line and comment the //next line //char ch; int fileSize=-1; fd = fopen(argv[1], "r"); do{ ch=getc(fd); //0xFF fileSize++; printf("fileSize=%d\n", fileSize); printf("Char read is ch=%c, in hex ch=%#hhx EOF is %#x\n\n", ch, ch, EOF); sleep(1); } while( ch != EOF); //ch =0x FF,...
Describe the general model of problem solving presented in the text. What are the three main...
Describe the general model of problem solving presented in the text. What are the three main phases and the different analysis step involved?
1) Code in python for solving the MNIST classification problem (for full size of the training...
1) Code in python for solving the MNIST classification problem (for full size of the training set Classify two digits from MNIST dataset 2) Logistic regression (LR) with L1 regularization where LR is differentiable, But L1 norm is not • Use proximal gradient descent • For L1 norm, that’s soft-thresholding • Use tensorflow library
Problem Solving/Goal Setting Checkpoint Provide an example of a time when you used the problem solving...
Problem Solving/Goal Setting Checkpoint Provide an example of a time when you used the problem solving and decision making. What is the role of creativity in the problem solving process? What are three ways that you can increase your personal creativity? Respond to the following in a essay: Describe a time you encountered a problem that required you to use the problem solving and decision making steps on page 150 of the textbook. How did you solve the problem using...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main(...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main( int argc, char* argv[] ) { // Size of vectors int n = 10000; // Input vectors double *restrict a; double *restrict b; // Output vector double *restrict c; // Size, in bytes, of each vector size_t bytes = n*sizeof(double); /* Q1: Allocate memory for vector a (10 points)*/ /* Q2: Allocate memory for vector b (10 points)*/ /* Q3: Allocate memory for vector...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT