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

Solution: Read and store filenames - C Tutorial

From the course: C: Data Structures, Pointers, and File Systems

Start my 1-month free trial

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…

Contents