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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
06-08-2005, 04:11 PM
|
#1
|
|
Member
Registered: Jun 2005
Location: Sacramento, CA
Distribution: Many, Old and New
Posts: 122
Rep:
|
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?
|
|
|
|
06-08-2005, 04:13 PM
|
#2
|
|
Guru
Registered: Jul 2003
Location: VA
Distribution: Slack 10.1
Posts: 2,194
Rep:
|
Use getenv("HOME") add that to the filename.
|
|
|
|
06-08-2005, 04:49 PM
|
#3
|
|
Member
Registered: Jun 2005
Location: Sacramento, CA
Distribution: Many, Old and New
Posts: 122
Original Poster
Rep:
|
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#
|
|
|
|
06-08-2005, 05:18 PM
|
#4
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530
Rep: 
|
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?
|
|
|
|
06-08-2005, 06:10 PM
|
#5
|
|
Member
Registered: Jun 2005
Location: Sacramento, CA
Distribution: Many, Old and New
Posts: 122
Original Poster
Rep:
|
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
|
|
|
|
06-08-2005, 06:40 PM
|
#6
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
...nevermind...
|
|
|
|
06-08-2005, 06:48 PM
|
#7
|
|
Member
Registered: Jun 2005
Location: Sacramento, CA
Distribution: Many, Old and New
Posts: 122
Original Poster
Rep:
|
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!
|
|
|
|
06-08-2005, 08:09 PM
|
#8
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
You could also do: sprintf(localfilename, "%s%s", getenv("HOME"), STATUSFILENAME);
|
|
|
|
06-08-2005, 11:10 PM
|
#9
|
|
Member
Registered: Jun 2005
Location: Sacramento, CA
Distribution: Many, Old and New
Posts: 122
Original Poster
Rep:
|
Quote:
Originally posted by itsme86
You could also do: sprintf(localfilename, "%s%s", getenv("HOME"), STATUSFILENAME);
|
Yup even better, one line.
|
|
|
|
06-09-2005, 01:46 AM
|
#10
|
|
Member
Registered: Sep 2004
Distribution: Debian, Ubuntu, BeatrIX, OpenWRT
Posts: 273
Rep:
|
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
|
|
|
|
06-09-2005, 09:53 AM
|
#11
|
|
Member
Registered: Jun 2005
Location: Sacramento, CA
Distribution: Many, Old and New
Posts: 122
Original Poster
Rep:
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:53 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|