0

I want to display the full path of all subfolders and files of a directory one per line in GCC. The existing questions don't provide a solution for listing the full path.

For eg, If I have a directory /root/abc which contains two files(a.txt and b.txt) and a directory xyz, then the code must display the following

/root/abc
/root/abc/a.txt
/root/abc/b.txt
/root/abc/xyz
Frank Meerkötter
  • 2,778
  • 2
  • 20
  • 26
pseudonym
  • 31
  • 9
  • 1
    Which language? Which platform? – Frank Meerkötter Mar 09 '16 at 07:02
  • @FrankMeerkötter C language in linux platform – pseudonym Mar 10 '16 at 08:59
  • @FrankMeerkötter Thanks for the edit ! :) – pseudonym Mar 10 '16 at 11:41
  • Notice that you actually want to list the full path not *inside* the [GCC](http://gcc.gnu.org/) compiler (that would not make much sense, but you might customize the compiler with [GCC MELT](http://gcc-melt.org/)...) but in a program *compiled by* GCC (and you could also compile it with another compiler, perhaps [Clang/LLVM](http://clang.llvm.org/)...) – Basile Starynkevitch Mar 10 '16 at 11:42
  • @BasileStarynkevitch Actually I want to list it inside the compiler itself as I'm using this as a utility function in my program. – pseudonym Mar 10 '16 at 11:55
  • Are you sure you want to change the behavior of the GCC compiler (e.g. when it would compile some other program, unrelated to yours)?? Compile-time (and compiler) is not the same as run-time (and your program)... – Basile Starynkevitch Mar 10 '16 at 11:57

1 Answers1

-1

Found the answer ! Just made a minor change to the answer given for this question Just print the "name" attribute along with "entry->d_name" inside the listdir function.

printf("%*s[%s%s]\n", level*2, "", name, entry->d_name);

Note: Make sure that the input you provide to the function doesn't have a trailing '/' because in that case you will get "//" instead of '/' in the full path.

For example, if the input is given is /root/abc/, the output will be

/root/abc//a.txt
/root/abc//b.txt
/root/abc//xyz
Community
  • 1
  • 1
pseudonym
  • 31
  • 9