Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

be thrown when things goes wrong; rather, it is C, and it is expected (in good programs, i.e., the only kind you'd want to write)

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

be thrown when things goes wrong; rather, it is C, and it is expected (in good programs, i.e., the only kind you'd want to write) that you always will check if the call succeeded. Reading the man page tells you the details of what is returned when an error is encountered; in this case, the macOS man page says: Upon successful completion fopen), fdopen (), freopen() and fmemopen() return a FILE pointer. Otherwise, NULL is returned and the global variable errno is set to indicate the error. Thus, as the code above does, please check that fopen() does not return NULL before trying to use the FILE pointer it returns Third, note that when the error case occurs, the program prints a message and then exits with error status of 1. In UNIX systems, it is traditional to return 0 upon success, and non-zero upon failure. Here, we will use 1 to indicate failure. Side note: if fopen() does fail, there are many reasons possible as to why. You can use the functions perror) or strerror) to print out more about why the error occurred; learn about those on your own (using you guessed it.. the man pages! Once a file is open, there are many different ways to read from it. The one we're suggesting here to you is fgets) , which is used to get input from files, one line at a time. To print out file contents, just use printf) . For example, after reading in a line with fgets) into a variable buffer, you can just print out the buffer as follows: printf("%s", buffer); Note that you should not add a newline (In) character to the printf), because that would be changing the output of the file to have extra newlines. Just print the exact contents of the read-in buffer (which, of course, many include a newline) Finally, when you are done reading and printing, use fclose) to close the file (thus indicating you no longer need to read from it) Details Your program my-cat can be invoked with one or more files on the command line; it should just print out each file in turn. In all non-error cases, my-cat should exit with status code 0, usually by returning a O from min) (or by calling If no files are specified on the command line, my-cat should just exit and return O. Note that this is slightly different If the program tries to fopen ( ) a file and fails, it should print the exact message "my-cat: cannot open file" (followed exit(0)). than the behavior of normal UNIX cat (if you're curious, figure out the difference) by a newline) and exit with status code 1. If multiple files are specified on the command line, the files should be printed out in order until the end of the file list is reached or an error opening a file is reached (at which point the error message is printed and my-cat exits)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions