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.
Solution: Read and store filenames - C Tutorial
From the course: C: Data Structures, Pointers, and File Systems
Solution: Read and store filenames
(upbeat music) - [Instructor] This type of challenge shows that it can be easy to do small things such as read a directory, but when you start combining these tasks into a larger project, the small things build into a ziggurat of complexity. Many different solutions are possible, of course, for this challenge, but here's what I did, broken into distinct parts. The first thing this code does is to open and create the output file at line 19. When opened at this point, the file is created in the program's directory, which is what I wanted. The first task assigned at line 27 is to fetch and massage user input. I use the fgets function at line 28, but it appends a new line to input, so the for loop at line 30 removes the new line and replaces it with a null character, effectively capping the string. The next assigned task is to change to the path that was input. The chdir function at line 40 accomplishes this task. If not, variable r does not equal zero. At this point, if that happens, the…
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
-
-
-