Aims and Objectives
Moving the file pointer
Handling File Errors
Determining the file type
ftell() takes a pointer to file and returns a long integer indicating the current file pointer or -1 if an error occurs. errno is set to indicate the error that occurred.
fseek() takes a file pointer, the new offset, and an integer which indicates how the new file position is to be calculated. whence can be one of:
SEEK_SET: set the file pointer to offset
SEEK_CUR: set the file pointer to current position +
offset
SEEK_END: set the file pointer to EOF + offset
fseek() will return an integer 0 if successful, and -1 otherwise. errno is set to indicate the error that occurred.
Sets the file pointer back to the beginning of the file. Equivalent to (void) fseek(FILE *stream, 0, SEEK_SET).
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.
int ferror(FILE *stream);
int feof(FILE *stream);
If feof() returns a value other than 0, use perror() to print the error message
If fgetc() and fputc() return an EOF, you must check that feof() returns 0 to be sure that an error has not occurred
To invoke a system call, such as to file, from within a C program, we can use the system() command
E.g., system("echo \"Hello there\"); corresponds to echo "Hello there" typed at the command line
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...