Skip to content
Advertisement

C – in recursive function remove() dont work

obviating if the recursive function is well constructed or not, when i call the function on a directory Folder/file.txt. this code should delete file.txt, but when it reaches to

JavaScript

remove dont work, and i dont know for what…

JavaScript

Advertisement

Answer

Reading the man page on readdir, it returns in d_name a file name, not a complete path, but remove and rmdir expect a full path (or an entry relative to current working directory).

So with every recursive call of your function, you must construct a new path of where you currently are, and pass that in your recursive call. (I leave to you to change your code to achieve this.)

Note: you also assume that if you can neither remove, nor rmdir, it must be a directory and you want to recurse. But it does not have to be a directory. You should check the d_type field.

Note: the man page for remove says it calls rmdir if the name is a directory. Calling rmdir is thus unnecessary.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement