PDA

View Full Version : [SOLVED] fopen() in C with directory



layers
February 11th, 2011, 11:05 PM
Alright, writing


FILE *fp = fopen("text","wb") will eventually be used to save a file named "text" in the directory where the executable is. What if I want to specify a relative or even an absolute directory. How would I be able to do it then?

FILE *fp = fopen("~/Desktop/folder/text","wb") does not work. // also...

cgroza
February 11th, 2011, 11:10 PM
Alright, writing


FILE *fp = fopen("text","wb") will eventually be used to save a file named "text" in the directory where the executable is. What if I want to specify a relative or even an absolute directory. How would I be able to do it then?

FILE *fp = fopen("~/Desktop/folder/text","wb") does not work. // also...

The "~" works only in bash. The standard library should provide a function to get the Homde Folder.

MadCow108
February 11th, 2011, 11:11 PM
relative and absolute directories work but the tilde(~) expansion is a shell feature and not directly available in C.

you'll have to write the path out (or, if available, use the environment variable HOME obtainable with getenv)

layers
February 11th, 2011, 11:16 PM
You're right, of course. Thanks.


fp=fopen("./folder/t", "wb")