LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-08-2005, 04:11 PM   #1
pentalive
Member
 
Registered: Jun 2005
Location: Sacramento, CA
Distribution: Many, Old and New
Posts: 124

Rep: Reputation: 15
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?
 
Old 06-08-2005, 04:13 PM   #2
aaa
LQ Guru
 
Registered: Jul 2003
Location: VA
Distribution: Slack 10.1
Posts: 2,194

Rep: Reputation: 47
Use getenv("HOME") add that to the filename.
 
Old 06-08-2005, 04:49 PM   #3
pentalive
Member
 
Registered: Jun 2005
Location: Sacramento, CA
Distribution: Many, Old and New
Posts: 124

Original Poster
Rep: Reputation: 15
Smile 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#
 
Old 06-08-2005, 05:18 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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?
 
Old 06-08-2005, 06:10 PM   #5
pentalive
Member
 
Registered: Jun 2005
Location: Sacramento, CA
Distribution: Many, Old and New
Posts: 124

Original Poster
Rep: Reputation: 15
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
 
Old 06-08-2005, 06:40 PM   #6
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
...nevermind...
 
Old 06-08-2005, 06:48 PM   #7
pentalive
Member
 
Registered: Jun 2005
Location: Sacramento, CA
Distribution: Many, Old and New
Posts: 124

Original Poster
Rep: Reputation: 15
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!
 
Old 06-08-2005, 08:09 PM   #8
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You could also do: sprintf(localfilename, "%s%s", getenv("HOME"), STATUSFILENAME);
 
Old 06-08-2005, 11:10 PM   #9
pentalive
Member
 
Registered: Jun 2005
Location: Sacramento, CA
Distribution: Many, Old and New
Posts: 124

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by itsme86
You could also do: sprintf(localfilename, "%s%s", getenv("HOME"), STATUSFILENAME);
Yup even better, one line.
 
Old 06-09-2005, 01:46 AM   #10
kees-jan
Member
 
Registered: Sep 2004
Distribution: Debian, Ubuntu, BeatrIX, OpenWRT
Posts: 273

Rep: Reputation: 30
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
 
Old 06-09-2005, 09:53 AM   #11
pentalive
Member
 
Registered: Jun 2005
Location: Sacramento, CA
Distribution: Many, Old and New
Posts: 124

Original Poster
Rep: Reputation: 15
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
a phantom file in my home dir microsoft/linux Debian 16 11-05-2005 10:54 PM
Not able to go to other user's home directory biswajit_dey Linux - Software 6 07-21-2005 08:14 AM
Konqueror: Icons/file names don't show up for home dir jlangelier Linux - Software 3 12-10-2004 02:54 PM
opening file in home dir with C++ qcoder Programming 1 07-05-2004 12:19 PM
howto make a dir shared that is not in my home dir Schmurff Linux - Newbie 2 06-19-2004 07:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration