• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Unix C++ question

Lazy8s

Golden Member
My program needs to find and delete a file and print out a specific message every time it does. Right now I am using:

system("find -name find_me.txt -delete 2>//dev//null");

The problem is this doesn't actually print anything out. What I have done is:

system("find -name find_me.txt 2>//dev//null 1>result.txt");
system("find -name find_me.txt -delete 2>//dev//null");

and I just print out "fine_me.txt was deleted" for every line in result.txt. There HAS to be a better way to do this! Any ideas? Unfortunately find returns 0 if all files are scanned correctly so I can't just print out a message based on this. I would really rather not write my own search and delete function. The problem with my implementation is if it finds a file that it can't delete it's going to print out a message for it because a line is added for every file found, whether you can delete it or not. Thanks in advance. I tried googling and it doesn't look real good.
 
If you really want to use the "find" program, look into pipes. You might also find the function "dup2" useful. Otherwise you could use the "stat" or "stat64" function. Another thing you could do is pipe the result to "wc" or "grep -c" - I think one of them has a mode to make the return code be the number of lines in the input.

There's probably a much better way to do what you're trying to do though - could you give us the full story on what you're doing?
 
Yeah, I have to find the file "find_me.txt" and delete it. Every time I delete the file I have to print out "find_me.txt was deleted". I am using a Linux machine. I was hoping to use the system call because it saves me from having to write the search and delete function on my own. I could easily do that, but this would save me some time and probably be more elegant.
 
Nothing that calls system() can be considered elegant. I don't know what the proper method would be in C++ but writing a recursive find function in C using the opendir(), readdir(), etc functions should only take like 15 minutes.
 
Back
Top