[SOLVED] open new or existing file and write to it. not working like I need it too.
ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
open new or existing file and write to it. not working like I need it too.
If I witre it that standard example way to open a file that works, but if I tell it to open a file using a path gotten from code, it does not work. the program does not fail (exit) on a file that is not there, it continues to run even thought it did not actually create and or open an existing file by the same name.
I get user home dir via what is in the code, add that path to a file name open the file, that is not there. it creates it (supposedly). but when I look it is not there at all.
Code:
struct passwd *p;
char *uhome = NULL;
uid_t uid;
if ((p = getpwuid(uid = getuid())) == NULL)
perror("getpwuid() error");
else {
uhome = p->pw_dir;
}
char configfile[21] = "/mhsetrootimagefile";
strcat(uhome,configfile);
FILE *fptr;
// this one not working -> //fptr = fopen("uhome", "wr+");
fptr = fopen("ThisFile", "w+");
if (fptr == NULL) {
fprintf(stderr,"cannot open config file\n");
exit(1);
}
printf("file = %s\n",uhome);
Code:
file = /home/userx/mhsetrootimagefile
[userx@FreeBSD12 ~]$ ls /home/userx/mhsetrootimagefile
ls: /home/userx/mhsetrootimagefile: No such file or directory
what is the proper way to cause the code to create if not there or open if there in a specified location?
IT is not because in here that code is commented out.
This will create a file called "uhome" in the current working directory. But if fptr==NULL, then perror(3) explains the problem.
I didn't think of looking for something like that.. and yep I just did and its there. Let me go scratch my head on this..
thanks..
back... that didnt take long..
Code:
fptr = fopen(uhome, "wr+");
remove quotes, it should show that how to open write to file thing in C is not my strong point.
thanks for pointing that out, now that part is working.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.