开发者

how to know the full path of new created file?

开发者 https://www.devze.com 2023-03-29 04:07 出处:网络
see i have 开发者_StackOverflow中文版make one program which create one new file just using fopen()

see i have 开发者_StackOverflow中文版make one program which create one new file just using

fopen()

Now i want to print full path of that file's in ma program.

How is that possible in linux with c programming?


Standard C by itself provides no way to do this, actually. But the POSIX standard has getcwd. What you would do is append the path used to create the file to the working directory path obtained from getcwd -- unless, of course, the path used to create the file is an absolute path.

Alternatively, you could use realpath.


You already have a file name, so the question is more how to detect whether it's already an absolute path or a relative path and turn the latter into an absolute path.

If it's an absolute path already, then the string will start with a /. If it's not an absolute path, then you know it's relative to your current working directory. Given that, you can determine the current working directory of your process and append the file path to that.

If you also want to resolve it to a real path (resolve symlinks, remove . and .. portions) you can use the realpath function.


In main() you should get the full path of your executable as argv[0]. Your file is created there.

Also, AFAIK, fopen prototype is

FILE * fopen(const char *restrict filename, const char *restrict mode);

So a plain

fopen() 

would not work (if we're talking about standard C).

0

精彩评论

暂无评论...
验证码 换一张
取 消