- Lab 2: File Operations and Unbuffered I/O
Preliminaries
Topics:
- Buffered vs Unbuffered I/O
- File operations
Part 1
What's the difference between buffered and unbuffered I/O? When to use which?
Problem:
- Write a program that reads test.txt in.
- Add 100 to the number in the file.
- Print out the new value.
- Example:
- $ echo "sum: 200" > test.txt
- $ cat test.txt
- $ ./my_program
- $ 300
1.1) Using Unbuffered I/O:
Download the code here
1.2) Using Standard I/O:
Can you rewrite the program using the following stdio routines?
- fopen
- fread
- fwrite
- fclose
Part 2
The fstat system call
It's used to extract the following information about a file:
- Size
- Type
- Number of links
- iNode #
- Permissions
- etc.
Find more about fstat here
Problem:
Let's write a program that prints out the above information about the file whose
name is passed to the program as an argument.
Download the code here
Author: Sasan Golchin