Question

Implement a program to display a files contents. It should be similar to the UNIX cat command when given a single file. NameGIVEN CODE. PLEASE FILL IN THE BLANK.

// Include Block
#include <fcntl.h>
#include <unistd.h>

// Preprocessor declarations
#define BUF_SIZE 10 /* global constant buffer size: Generally this would be much larger, but I want to show you that it works in a loop until the entire file is read.*/

// main function
int main(int argc, char *argv[])
{
   // Variable declarations
   int fd;                       // file descripter
   char buffer[BUF_SIZE];       // string for buffer between read and write
   long n;                       // amount read by read()

   if (____ != 2)
   {
       write(STDOUT_FILENO, "Usage: show filename\n", 21);
       return 1;
   }

   // Open the file given as an argument by the user
   if ((____ = open(____, O_RDONLY)) == -1 )
   {
       // Report error opening
       write(STDERR_FILENO, "Error opening file.\n", 20);
       return 1;
   }

   // Loop through until nothing is read
   while ( (____ = read(____, ____, ____)) > 0 )
   {  
       // Write what was read to the stdout
       if( write(STDOUT_FILENO, ____, ____) != n )
       {
           //report error writing
           write(STDERR_FILENO, "Error writing character.\n", 25);
           return 3;
       }
   }

   // Error check
   if(____ == -1)
   {
       // report error reading
       write(STDERR_FILENO, "Error reading character.\n", 24);
       return 2;
   }

   /* If we got here that means n=0, meaning it successfully read and wrote
   the whole file without error, and there is nothing left to read. */

   // Close the file
   close(____);

   // Success:
   return 0;
}

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

--> Below is the program with blanks filled appropriately in bold letters

Code:

// Include Block
#include <fcntl.h>
#include <unistd.h>

// Preprocessor declarations
#define BUF_SIZE 10

/* global constant buffer size: Generally this would be much larger,
but I want to show you that it works in a loop until the entire file is read.*/

// main function
int main(int argc, char *argv[])
{
   // Variable declarations
   int fd;                       // file descripter
   char buffer[BUF_SIZE];       // string for buffer between read and write
   long n;                       // amount read by read()

   if (argc != 2)   //checking argument count
   {
       write(STDOUT_FILENO, "Usage: show filename\n", 21);
       return 1;
   }

   // Open the file given as an argument by the user
   if ((fd = open(argv[1], O_RDONLY)) == -1 )
   {
       // Report error opening
       write(STDERR_FILENO, "Error opening file.\n", 20);
       return 1;
   }

   // Loop through until nothing is read
   while ( (n = read(fd, buffer, BUF_SIZE)) > 0 )
   {
       // Write what was read to the stdout
       if( write(STDOUT_FILENO, buffer, n) != n )
       {
           //report error writing
           write(STDERR_FILENO, "Error writing character.\n", 25);
           return 3;
       }
   }

   // Error check
   if(n == -1)
   {
       // report error reading
       write(STDERR_FILENO, "Error reading character.\n", 24);
       return 2;
   }

   /* If we got here that means n=0, meaning it successfully read and wrote
   the whole file without error, and there is nothing left to read. */

   // Close the file
   close(fd);

   // Success:
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
GIVEN CODE. PLEASE FILL IN THE BLANK. // Include Block #include <fcntl.h> #include <unistd.h> // Preprocessor...
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
  • GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h>...

    GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() /// chi read x ---> divi ---> write x into file ---> par read x --> sub--> write x into file---> chi read x-->etc {    int pid;           // pid: used to keep track of the child process    int x = 19530;       // x: our value as integer   ...

  • Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this...

    Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this problem. You can still read/write from/to a file. You must use ( kill() and pause() ) system calls. rewrite code below to do kill and pause system calls #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() { int pid; // pid: used to keep track of the child process int x...

  • /* BEGIN - DO NOT EDIT CODE */ // File: main.cpp #include "LoginAccount.h" #include "RegisteredLoginAccount.h" #include...

    /* BEGIN - DO NOT EDIT CODE */ // File: main.cpp #include "LoginAccount.h" #include "RegisteredLoginAccount.h" #include "GuestLoginAccount.h" #include <iostream> #include <fstream> #include <string> #include <sstream> using namespace std; //prototypes and constants const int ARRAY_SIZE = 20; using number_of_records_t = int; //Function Declarations. void readAccountsFromFile(ifstream& fin, RegisteredLoginAccount registeredAccounts[], number_of_records_t& registeredLength, GuestLoginAccount guestAccounts[], number_of_records_t& guestLength); bool openFileForInput(ifstream& ifs, const string& filename); bool openFileForOutput(ofstream& ofs, const string& filename); void writeAccountsToFile(ofstream& ofs, RegisteredLoginAccount registeredAccounts[], const number_of_records_t registeredLength, GuestLoginAccount guestAccounts[], const number_of_records_t guestLength); void writeAccountToFile(ofstream&...

  • #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int...

    #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...

  • I need help fixing this code in C. It keeps giving me an error saying error:...

    I need help fixing this code in C. It keeps giving me an error saying error: ‘O_RDWD’ undeclared (first use in this function) if ((fd = open(fifo, O_RDWD)) < 0) note: each undeclared identifier is reported only once for each function it appears in. Please fix so it compiles properly. //************************************************************************************************************************************************** #include <fcntl.h> #include <stdio.h> #include <errno.h> #include<stdlib.h> #include <string.h> #include<unistd.h> #include <sys/stat.h> #include <sys/types.h> #define MSGSIZE 63 char *fifo = "fifo"; int main(int argc, char **argv){    int fd;...

  • Need this in C The starter code is long, if you know how to do it...

    Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...

  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

  • 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...

  • Error: Unable to save the file in the folder...... Whats is wrong with this code.? The...

    Error: Unable to save the file in the folder...... Whats is wrong with this code.? The below code checks for the HTTP/1.1 200 OK. and if its true it divides tha pathname and hostname and after first slash in the pathname, it saves as the downloading link and have to save in the folder. but it 's not getting saved... int bytes sent; bytes sent send(sockfd, request strlen(request),0) int byte received; char buffer 10000J; bzero buffer 10000); //to make sure...

  • Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h>...

    Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<dirent.h> #include<stdio.h> #include<stdlib.h> void do_ls(char []); int main(int argc,char *argv[]) { if(argc == 1) do_ls("."); else while(--argc){ printf("%s:\n",*++argv); do_ls(*argv); } } void do_ls(char dirname[]) { DIR *dir_ptr; struct dirent *direntp; if((dir_ptr = opendir(dirname)) == NULL) fprintf(stderr,"ls1:cannot open %s\n",dirname); else { while((direntp = readdir(dir_ptr)) != NULL) printf("%s\n",direntp->d_name); closedir(dir_ptr); } } ____________________________ code 2: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> void show_stat_info(char *,...

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