Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C, how to I get to a specified directory?

I have to do a program where I need to index the files in a specified directory. I've gotten the indexing part down, but what I'm having trouble with is how to navigate to the directory. For example, say when I start the program, it will ask "What directory would you like to index," And then the input would be "usr/Documents/CS/Assignment4," how do I get to the "Assignment4" directory? I know recursion is needed, but I'm really confused as to how directories work in C. Say my source file is in "usr/Documents/SourceCode," then what should I do to get to Assignment4?

I know I sound like I want all the answers, but I'm completely lost as to how directories work, and the book I have sucks. So even if all you have is a link to a good tutorial on this, that would be fantastic.

I'm running Linux, Ubuntu to be exact. GCC is the compiler.

like image 790
sx2000 Avatar asked Oct 26 '10 00:10

sx2000


1 Answers

The C programming language doesn't have a notion of a file system. This is instead an operating system specific question.

Based on the style of directory in your question though it sounds like you're on a unix / linux style system. If that's the case then you're looking for the opendir function

  • http://linux.die.net/man/3/opendir
like image 61
JaredPar Avatar answered Oct 05 '22 00:10

JaredPar