Warm tip: This article is reproduced from serverfault.com, please click

how to exclude the running executable C

发布于 2020-11-28 22:27:09

I'm writing a program dealing with directories and files. This program is going to open all accessible directories and read all accessible files under user input initial path.

The question is if the program opened the running executable, I will get seg fault, I'm wondering if there is any way that I could exclude the executable which is running but includes all other files? Given the condition that the program knows the name of executable but not knowing where the executable is gonna be nor the initial path

Questioner
Boba
Viewed
0
Joshua 2020-11-29 08:34:09

To fix you problem, check the return value of fopen(). If you can't open the file it returns NULL and puts a reason into errno. You can get the error message string with strerror(). Consider using perror() to output the error message as you loop through the files.

There's other possibilities like not handling really long lines, but this is by far the most likely problem.

This doesn't get the executable, but fixes the general segfault problem you're having which is a much more reasonable thing to do anyway.