Programming in C

Lecture 10: File Manipulation (2)

CHRIS STAFF
Dept. of Computer Science and Artificial Intelligence
University of Malta


Lecture Outline

Aims and Objectives
Moving the file pointer
Handling File Errors
Determining the file type


Aims and Objectives

Moving the file pointer

Exercise

Open a file which doesn't exist (or overwrite an existing file) for read/writing.

Print the initial value of the file pointer. Set the file pointer to the end of the file, and printer the current value of the file pointer. Is the new file pointer different from the initial one?

Set the file pointer to 50 beyond the end of file, write a character, go back to the beginning of the file and print the values of each byte in the file (you may have to print the ASCII values of the bytes to see them).

Try to read beyond the EOF marker, printing the character read.

Try to read before the beginning of the file, printing the character read.

Handling file errors

Determining the File Type

Exercise

The UNIX file command simply prints the file type to standard output. What UNIX command can be used to determine if the output contains a specific string, e.g., "text"?

How can we conjoin the two UNIX commands into a single instruction?

How can we prevent or dispose of the output from the conjoined command, so that it doesn't appear on the screen?

How can we ensure that the string we're searching for is at the end of the output from the conjoined command? E.g., looking for the string "text" in

text.bin: ELF 32-bit MSB executable SPARC Version 1, dynamically linked, not stripped

will incorrectly identify the file as a text file...