C - Opening a Status file in user's home dir.
ansi GCC "C"
I am trying to save a status file as a hidden file in the user's home directory. ======================= Code:
FILE *fp; When I do this I get a segmentation error. If I take out the "~/" everything's ok... Tried "~//.statusfile" no go , still get a segmentation error. How do I put the file into the user's home directory and make it hidden? |
Use getenv("HOME") add that to the filename.
|
Thats got it, Thanks!
Ok, Thats got it...
I suppose I was wanting the shell to expand ~ and the shell was not involved. Thanks.http://www.linuxquestions.org/questi...readid=331646# |
But how come the ~ makes it segfault, I'm wondering.
If ~ is illegal in filenames, fopen() should return error, not segfault. What could be the reason? |
Quote:
Code:
and that goofed me up for a short time. It's for an RPN calculator program that binds the keyboard in (what I hope is) a logical manner. I bind the keys of the imbedded numeric keypad to numbers and the functions to the other keys |
...nevermind...
|
Yeah, I know I could have made the strcpy get the environment and then only had to strcat my filename. I'll probably change it at some point..
It works now. Cool. Thanks! |
You could also do: sprintf(localfilename, "%s%s", getenv("HOME"), STATUSFILENAME);
|
Quote:
|
To improve even further, you could add error checking. The segfault you had earlier was probably because you didn't check if the fopen succeeded at all. Similarly, getenv() may not find the "HOME" environment variable (unlikely, but possible), and sprintf might result in a pathname that is too long for the buffer. Use snprintf instead.
Good luck, Kees-Jan |
Thanks,
Actually at one point I did ... well f = fopen... if (f == NULL) { print somthing out } but I never got to the "print somthing out" part... I thing I was segfaulting in fopen(?) anyway I'll add this stuff to the todo list. anyone who wants a copy of this please email me.. uh, can you get my email addresss.. oh Here it is: ron dot hudson at sbc global (one word) dot net |
All times are GMT -5. The time now is 12:52 AM. |