Question

UNIX is all about manipulating files and input/output streams fluidly, so it is important to get a strong grasp of how this fPART 2: filecat Modify the cat implementation in Part 1 to use files instead of standard input and output. Usage should be as

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the program to read and write to output file. Also i have added necessary comments in it.

Program:

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

enum { BUFFER_SIZE = 16 };
int main(int argc, char *argv[])
{
   int n;
   unsigned char read_char[BUFFER_SIZE]={'\0'};
   int fptr, fptw;

   /* check for format */
   if(argc != 3)
   {
     printf("Usage: %s <input_file> <output_file>\n", argv[0]);
     return -1;
   }

   /* open the file */
   if ((fptr = open(argv[1], O_RDONLY )) < 0)
   {
      printf("Error: %s %s\n",argv[1], strerror(errno));
      return -1;
   }

   /* new file wil be created if output file is not found,
    * if output file found, content will be appended to outfile. */
   if ((fptw = open(argv[2], O_CREAT |O_APPEND|O_WRONLY) ) < 0)
   {
      printf("Error: %s %s\n",argv[2], strerror(errno));
      return -1;
   }

   if(fchmod(fptw, 0) == -1)
   {
      printf("Error: %s %s\n",argv[1], strerror(errno));
      return -1;
   }

   /* change the file mode to -rw-r----- */
   if(fchmod(fptw, S_IRUSR |S_IWUSR |S_IRGRP ) == 1)
   {
      printf("Error: %s %s\n",argv[1], strerror(errno));
      return -1;
   }
   /* read the file until read returns 0 */
   n= read(fptr, read_char, BUFFER_SIZE);

   if (n == -1)
   {
      printf("Error: %s\n", strerror(errno));
      return -1;
   }

   /* writing to the file */
   while (n != 0)
   {
      if(n < 16)
      write(fptw, read_char, n);
      else
      write(fptw, read_char, BUFFER_SIZE);

      bzero(read_char, BUFFER_SIZE);

      /* read the object file until fread returns less than 1 */
      n= read(fptr, read_char, BUFFER_SIZE);

      if (n == -1)
      {
         printf("Error: %s\n", strerror(errno));
         return -1;
      }
   }

/* close both the files */

   close(fptr);
   close(fptw);
   return 0;
}

Output:

USER>./filecat exoplanets.dat
Usage: ./filecat <input_file> <output_file>
USER>

USER>cc file_cat.c -o filecat

USER>./filecat exoplanets.dat output_file


USER>ls -l output_file
-rw-r----- 1 asgu warp 100 Jun 4 10:03 output_file


USER>cat output_file
aaaaaa 10 3.4
bbbbbb 20 5.6
cccccc 340 7.86
dddd 230 4.574
eeee 5 7.8
ggggg 344 5.2
hhhhh 780 3.45


USER>cat exoplanets.dat
aaaaaa 10 3.4
bbbbbb 20 5.6
cccccc 340 7.86
dddd 230 4.574
eeee 5 7.8
ggggg 344 5.2
hhhhh 780 3.45

#following output shows file contents are appended

USER>./filecat exoplanets.dat output_file
USER>
USER>
USER>ls -l output_file
-rw-r----- 1 asgu warp 200 Jun 4 10:07 output_file
USER>
USER>cat output_file
aaaaaa 10 3.4
bbbbbb 20 5.6
cccccc 340 7.86
dddd 230 4.574
eeee 5 7.8
ggggg 344 5.2
hhhhh 780 3.45
aaaaaa 10 3.4
bbbbbb 20 5.6
cccccc 340 7.86
dddd 230 4.574
eeee 5 7.8
ggggg 344 5.2
hhhhh 780 3.45
USER>

Add a comment
Know the answer?
Add Answer to:
UNIX is all about manipulating files and input/output streams fluidly, so it is important to get a strong grasp of how...
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
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
ADVERTISEMENT