LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C - Opening a Status file in user's home dir. (https://www.linuxquestions.org/questions/programming-9/c-opening-a-status-file-in-users-home-dir-331646/)

pentalive 06-08-2005 04:11 PM

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;

fp = fopen("~/.statusfile","w");

  /* write stuff */
  fprintf(fp,"%d\n",stuff);

fclose(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?

aaa 06-08-2005 04:13 PM

Use getenv("HOME") add that to the filename.

pentalive 06-08-2005 04:49 PM

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#

Hko 06-08-2005 05:18 PM

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?

pentalive 06-08-2005 06:10 PM

Quote:

Originally posted by Hko
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?

what I am doing now:

Code:


#define STATUSFILENAME "/.calculastatus"

char localfilename[80];

strcpy(localfilename,"");  /* Clear local filename */
strcat(localfilename,getenv("HOME"));    /* get home dir */
strcat(localfilename,STATUSFILENAME);  /* append my filename */

I found that the localfilename string was filled with lots of nice junk
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

itsme86 06-08-2005 06:40 PM

...nevermind...

pentalive 06-08-2005 06:48 PM

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!

itsme86 06-08-2005 08:09 PM

You could also do: sprintf(localfilename, "%s%s", getenv("HOME"), STATUSFILENAME);

pentalive 06-08-2005 11:10 PM

Quote:

Originally posted by itsme86
You could also do: sprintf(localfilename, "%s%s", getenv("HOME"), STATUSFILENAME);
Yup even better, one line.

kees-jan 06-09-2005 01:46 AM

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

pentalive 06-09-2005 09:53 AM

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.