From 66404cf75a75bf34209806bb4d539f97ef44cf72 Mon Sep 17 00:00:00 2001 From: Remzi Arpaci-Dusseau Date: Tue, 16 Jan 2018 13:19:33 -0600 Subject: [PATCH] more my-cat --- initial-utilities/README.md | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/initial-utilities/README.md b/initial-utilities/README.md index d955ac9..3a53016 100644 --- a/initial-utilities/README.md +++ b/initial-utilities/README.md @@ -22,14 +22,16 @@ these skills already, this is not the right place to start. ## my-cat The program **my-cat** is a simple program. Generally, it reads a file as -specified by the user and prints its contents. A typical usage is as follows: +specified by the user and prints its contents. A typical usage is as follows, +in which the user wants to see the contents of main.c, and thus types: ``` -my-cat main.c +prompt> my-cat main.c +#include +... ``` -In this case, **my-cat** will read the file **main.c** and print out its -contents. +As shown, **my-cat** reads the file **main.c** and prints out its contents. You'll need to learn how to use a few library routines from the C standard library (often called **libc**) to implement the source code for this program, @@ -60,7 +62,23 @@ learning now? We will also give a simple overview here. The **fopen()** function "opens" a file, which is a common way in UNIX systems to begin the process of file -access. +access. In this case, opening a file just gives you back a pointer to a +structure of type **FILE**, which can then be passed to other routines to +read, write, etc. + +Here is a typical usage of **fopen()**: + +```c +FILE *fp = fopen("main.c", "r"); +if (fp == NULL) { + printf("cannot open file\n"); + exit(1); +} +``` + + + +