IITDU Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

File Copy Homework, 4 november, 2010

3 posters

Go down

File Copy Homework, 4 november, 2010 Empty File Copy Homework, 4 november, 2010

Post by BIT0102-Mohaimin Thu Nov 04, 2010 1:59 pm

This is the copy homework which is supposed to submitted today.
Its in C.

Code:

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

int main(int args, char* arg[]){
   if (args < 4){
      puts("To few arguments");
      return args;
   }else if(args > 4){
      puts("To many arguments");
      return args;
   }
   
   char buffer[1024];
   
   size_t nbytes = atoi(arg[3]);
   ssize_t readByte, writeByte;
   int writeFile,readFile;
   
   readFile = open(arg[1], O_RDONLY);
   if(errno!=0){
      printf("Exit with error code %d\n", errno);
      printf("\tCould not open file %s\n", arg[1]);
      return errno;
   }
   
   readByte = read(readFile, buffer, nbytes);
   if(errno!=0){
      printf("Exit with error code %d\n", errno);
      printf("\tCould not read file %s\n", arg[1]);
      close(readFile);
      return errno;
   }
   
   if(readByte==-1){
      printf("Exit with error code %d\n", readByte);
      printf("Nothing to read\n");
      close(readFile);
      return errno;
   }
   
   writeFile = open(arg[2], O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
   if(errno!=0){
      printf("Exit with error code %d\n", errno);
      printf("\tCould not open file: %s\n", arg[2]);
      return errno;
   }
   
   writeByte = write(writeFile, buffer, nbytes);
   if(errno!=0){
      printf("Exit with error code %d\n", errno);
      printf("\tCould not write to file: %s\n", arg[2]);
      close(writeFile);
      return errno;
   }
   
   if(writeByte==-1){
      printf("Exit with error code %d\n", writeByte);
      printf("\tNothing to write\n");
      close(writeFile);
      return errno;
   }
   
   close(readFile);
   close(writeFile);
   
   return 0;
}

Hope you will not just copy and paste.
BIT0102-Mohaimin
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

Back to top Go down

File Copy Homework, 4 november, 2010 Empty Re: File Copy Homework, 4 november, 2010

Post by BIT0119-Asif Thu Nov 04, 2010 2:09 pm

rep++
BIT0119-Asif
BIT0119-Asif
Study Moderator
Study Moderator

Course(s) :
  • BIT

Blood Group : B+
Posts : 163
Points : 432

Back to top Go down

File Copy Homework, 4 november, 2010 Empty Re: File Copy Homework, 4 november, 2010

Post by BIT-0126 Fri Nov 05, 2010 3:09 am

C দিয়ে করলে কি হবে???

BIT-0126
Alpha Release
Alpha Release

Course(s) :
  • BIT

Blood Group : AB+
Posts : 43
Points : 68

Back to top Go down

File Copy Homework, 4 november, 2010 Empty Re: File Copy Homework, 4 november, 2010

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum