From the course: C: Data Structures, Pointers, and File Systems
Unlock the full course today
Join today to access over 22,500 courses taught by industry experts or purchase this course individually.
Getting file information - C Tutorial
From the course: C: Data Structures, Pointers, and File Systems
Getting file information
- 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. And you see that the stat()…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
Reading from a file3m 25s
-
(Locked)
Writing to a file3m 24s
-
(Locked)
Working with raw data2m 53s
-
(Locked)
Using random file access2m 47s
-
(Locked)
Manipulating files2m 57s
-
(Locked)
Exploring the path2m 8s
-
(Locked)
Reading a directory2m 53s
-
(Locked)
Getting file information2m 46s
-
(Locked)
Using command line arguments2m 44s
-
(Locked)
Calling the operating system2m 46s
-
(Locked)
Challenge: Read and store filenames1m 6s
-
(Locked)
Solution: Read and store filenames2m 52s
-
-
-