The stat() function returns information about a file. The second argument of the stat() function is a stat structure pointer, into which file information—from the filename as the first argument—is stored. Members of the stat structure give file type, file size, file timestamps, and so on. The stat() function's main page must be reviewed to see which stat structure members are available for a given file system.
- To gather information about a file, … it's type, size, timestamps, and other trivia, … you use the stat() function. … This function is prototyped in the sys/stat header file … which is included in this exercise file at line two. … The stat() function requires two arguments … as shown on line 10. … A filename string, and I'm using alpha.txt … which is included with the exercise files … and it's declared as a string constant at line seven. … The second argument to the stat() function … is the address of a stat() structure, … fstat here, which is declared at line eight. … Two stat() structure members are accessed for output. … The first printf statement at line 11 … access the st_size member which lists the file size. … The second printf statement at line 15 … accesses the st_mtime member … which contains a clock tick value … representing when the file was last modified. … Here at line 15, I'm using the ctime function … to convert the clock tick value into a displayable string. … Build and run. …
- Working with arrays
- Building a structure
- Creating an array of structures
- Testing characters
- Working with strings in C
- Using pointers to manipulate data
- Manipulating files
- Using command-line arguments
- Working with time functions
Skill Level Intermediate
1. Arrays and Structures
2. Characters and Strings
4. Files and the Operating System
5. The Interesting and Extraordinary
Next steps1m 52s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.