2021年1月25日星期一

Read a file in /proc using an absolute path?

I'm trying to read a file that is located in proc/PID/environ which is 5 folder higher in the tree of folder than my current program.

I can do it when I write the following file name "../../../../../proc/PID/environ", but I would like the program to be more portable (compatible) with other computers.

The file is not recognized when I use "/proc/PID/environ", which is the absolute path of the file.

Here is the lines of code where it seems to not work:

int main(int argc, char *argv[]) {          char filename[strlen(argv[2])];          char* value;          int key = 579;          char line[1000];          int nDigits = floor(log10(abs(key))) + 1;          snprintf(filename, nDigits+15, "/proc/%d/environ", key);              FILE *file = fopen(filename, "r");  

The error is simply that the file doesn't exist. Error : errno='No such file or directory'.

filename if printed is really /proc/579/environ so I don't see the problem

Is there a way to read a file in C that is not in the current directory, without using ".." to go back in the tree? /proc/PID/environ being at the root of the tree, I would like to be able to use the absolute path.

https://stackoverflow.com/questions/65895559/read-a-file-in-proc-using-an-absolute-path January 26, 2021 at 11:37AM

没有评论:

发表评论