LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 10-14-2019, 01:57 PM   #1
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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.

Last edited by BW-userx; 10-14-2019 at 01:58 PM.
 
Old 10-14-2019, 02:29 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,865
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
fptr = fopen("uhome", "w+");
This will create a file called "uhome" in the current working directory. But if fptr==NULL, then perror(3) explains the problem.

Last edited by NevemTeve; 10-14-2019 at 02:35 PM.
 
1 members found this post helpful.
Old 10-14-2019, 02:44 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by NevemTeve View Post
Code:
fptr = fopen("uhome", "w+");
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.

Last edited by BW-userx; 10-14-2019 at 02:48 PM.
 
  


Reply



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
[SOLVED] How to Write a New Distro, or Modify and Existing to Make it My Own. powerpc Linux From Scratch 5 11-04-2014 08:48 PM
[SOLVED] Write a script that searches a txt file and write's specific data to a new file tomtweber Programming 22 02-22-2014 12:36 PM
LXer: My Nerd Life: Too Loud, Too Funny, Too Smart, Too Fat LXer Syndicated Linux News 0 01-24-2014 05:21 AM
read/write,write/write lock with smbclient fails swatidas11 Linux - Networking 1 03-10-2010 12:27 PM
Need to find the right distro whichs not like slckwre but not too limited like mndrke mikeymorgan Linux - Distributions 14 11-01-2005 10:06 PM

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

All times are GMT -5. The time now is 08:55 AM.

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