CSC161 2011S Imperative Problem Solving

Reading: Files

Reading: K&R 7.5-7.7

Please submit a question by 9 p.m. on Monday, 18 April 2011.


Basic Questions

What exactly is FILE *fp?

fp is a pointer to a file. That is, it's something we can use to read and write output. Typically, it's a position in a real file. But it can also be a pseudo-file, such as stdin or stdout.

I meant to say What type is FILE *fp? It's a file pointer but how does that work?

The FILE thingy contains information about the real file or pseudo-file: Perhaps how to tell the operating systems to read from it or write to it (probably function pointers), where in the file you are, a buffer of partially-read or partially-written text used to increase speed, a unique integer used to describe the file, and so on and so forth. But the whole point here (and elsewhere) is to conceal irrelevant details from the client - you just need to know how to use them, not how they are implemented.

Does dereferencing a file pointer ever give you anything meaningful, or is it just for the implementation of getc, putc, and such?

It is primarily for the implementation of a variety of functions. (There is someting meaningful there, but it's a bit more complex than you can understand at this point in your career.)

What does it mean that the file pointers stdin and stdout are objects of type FILE *? [p. 162]

It simply means that you can use them where you would use other file pointers. So, you can use stdin whenever you want to read values and stdout whenever you want to write values.

What does stderr do?

What does stderr do in C?

The stderr value gives you a way to print error messages and to separate them from normal output.

Why does opening an existing file for writing cause the old contents to be discarded? 161. What if we want to edit the file?

If you want to modify an existing file, you need to go beyond the basic C file operations (and, it often seems, the basic Unix file operations). Basically, you'll need to read the whole file into memory, modify it in memory, and then write it back to disk.

What does it mean that gets deletes the terminal '\n'?

As you may recall, a line of input ends with the carriage return character, \n. gets (and fgets) read the carriage return, but don't put it in the result string.

In 7.5, it says fscanf and fprintf can be used to format input or output files. The second argument for these functions is a format string. What does format mean here?

It means the same thing that it does for the regular printf and scanf. That is, the format string is a pattern that describe how the output (or input) should be formatted.

What is the difference between getchar and getc?

getc reads from any file. getchar can only read from stdin.

What is the difference between putchar and putc?

putc outputs to any file. putchar can only output to stdout.

What is the difference between using the commands we learned in this section (getc, putc, fopen) and those we learned before (getchar, putchar, and the >/< operators)?

Now you get to put a file pointer in as a parameter. Now you can choose the file you work with from within the program (which means that you can work with multiple files).

What is mode in this code:

fp = fopen(name, mode);

A string that indicates how you want to use the file. It can be "r" (for read), "w" (for write), or "a" (for append).

I don't understand how fopen and fclose work.

You don't need to understand how they work, you need to understand what they do. fopen takes a file name and gives you something you can use to manipulate the file. fclose says that you're done with the file.

What do the file permissions have to be for a program to write to a file?

The file has to be writable by the current user. So, if it's your file, it should be 644 or 666 or 600 or 700 or .... If it's someone else's file, then the group or other field must have the write flag set. In addition, you probably need write permission to the directory.

Can we go through the cat program together in class?

We'll go through lots of parts in lab.

The first paragraph in 7.6 says the treatment of errors in cat is not ideal. Could you re-explain why this is so?

I think they explain in the next sentence: We need to distinguish between the normal output form cat and the error messages produced by cat.

Can you explain more in depth on how fopen and its pointers work?

That's it a bit open-ended. Basically, fopen opens a file for reading or writing and returns a pointer to information on the file (including the position in the file).


Questions On Other Topics

Could you further explain giving the exit function particular arguments, like 2 and 0?

If you exit with a value of 0, you mean the program has finished normally. If you exit with any other value, you mean that the program has terminated abnormally. The particular value can be specific to the individual program, although there are some standards.

When exactly do we use exit instead of return?

Would you recommend using exit(expr) instead of return expr from this point forward?

I find that return is much cleaner. I would not recommend using exit.

How does the function exit work?

It stops the program and returns a value to the operating system.

Does exit print out the number given to distinguish between the other two exit points?

No. But it's possible to trap it at the OS level.

Why does fgets return NULL if there is an error, when on the same page authors say that conventionally, a return value of 0 signals that all is well? [p. 164]

There's a difference between the return value from main and the return value from other functions, such as fgets. We use 0 for okay in main because we may want to use the exit value to indicate what went wrong, and zero-nonzero is an easy distinction to make. For everything else, we use true (nonzero) as success and false (0, NULL) as failure.

Why are int c and char *cs declared with register? [p. 165]

You can ignore this modifier. Basically, it says put it in the fastest possible memory, because I'll be using it a lot.


Questions I Have Difficulty Parsing

Is the stdout always appended to unless you clear the buffer?

If you use printf or the other normal output routines, output goes to standard output. I'm not sure what clearing the buffer has to do with it.

I don't really understand fget. Can we go over it?

I don't know what fget is. There's fgets (for strings) and fgetc (for chars).


Questions Left to Answer


The first five people to tell me that they've read to this point will receive a bit of extra credit, enough to make up for one missed reading response.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue Apr 19 10:42:50 2011.
The source to the document was last modified on Tue Apr 19 10:42:46 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Readings/files.html.

Samuel A. Rebelsky, rebelsky@grinnell.edu