LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-02-2005, 07:04 AM   #1
igor@44
Member
 
Registered: Jun 2005
Distribution: Slackware 10.2
Posts: 32

Rep: Reputation: 15
( C++ ) - How to find username?


I need to write program that writes something into /home/{username}/... but I don't know how to make program find that username. I'm writting this program in C++
 
Old 08-02-2005, 08:42 AM   #2
maroonbaboon
Senior Member
 
Registered: Aug 2003
Location: Sydney
Distribution: debian
Posts: 1,495

Rep: Reputation: 48
Use the 'getenv' system call to get the value of the environment variable USER.
 
Old 08-03-2005, 05:19 AM   #3
igor@44
Member
 
Registered: Jun 2005
Distribution: Slackware 10.2
Posts: 32

Original Poster
Rep: Reputation: 15
Can you give me so me example for "getenv"?

Maybe something like this:
Code:
var env_user    = getenv("USER");
 
Old 08-03-2005, 07:15 AM   #4
maroonbaboon
Senior Member
 
Registered: Aug 2003
Location: Sydney
Distribution: debian
Posts: 1,495

Rep: Reputation: 48
Sorry, I'm not a C++ user, but here is a complete C program:
Code:
#include <stdlib.h>
main() 
{
  printf(getenv("USER"));
}
 
Old 08-03-2005, 07:19 AM   #5
coolguy_iiit
Member
 
Registered: Aug 2004
Posts: 51

Rep: Reputation: 15
dude check man getenv


if u still dont know how to use getenv...try posting ur problem in linux-programming..



bye
 
Old 08-03-2005, 09:28 AM   #6
geeman2.0
Member
 
Registered: Feb 2005
Location: Ontario, Canada
Distribution: Gentoo, Slackware
Posts: 345

Rep: Reputation: 30
Couldn't you just write to "~/filename"
I know in the shell ~ gets mapped to /home/username.
Would this functionality carry over into c++?
 
Old 08-03-2005, 10:01 AM   #7
KimVette
Senior Member
 
Registered: Dec 2004
Location: Lee, NH
Distribution: OpenSUSE, CentOS, RHEL
Posts: 1,794

Rep: Reputation: 46
Several ways to get the home directory:

$HOME
~
just "cd" with no argument (if you're doing a shellexec or equivalent)

several ways to get the user:

whoami
$USER
(see source for "whoami" to see what calls that program does if shellexec or equivalent is not for you)
 
Old 08-03-2005, 02:11 PM   #8
igor@44
Member
 
Registered: Jun 2005
Distribution: Slackware 10.2
Posts: 32

Original Poster
Rep: Reputation: 15
I wanted to make C++ program that copies itself into ~/.kde/Autostart. Here is my code:
Code:
main() 
{
CopyFile("~/Desktop/what.txt, "~/Dekstop/what3.txt", false);
}
But when I try to compile this in gcc (can gcc compile C++ code?) it says me:
autostart.c: In function `main':
autostart.c:3: error: parse error before '~' token
autostart.c:3:51: missing terminating " character
What can I do now?
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why "bash-2.05b$" in Terminal instead of "username@localhost username" jayelitumbiolo Linux - General 9 10-04-2009 03:16 AM
find out email address from username syrotkin Linux - Networking 2 10-20-2005 04:32 PM
Can I change my username? ssobeht LQ Suggestions & Feedback 19 10-14-2005 09:03 AM
useradd: invalid username username$ engyeow Fedora 5 12-05-2004 04:35 AM
How to login w/o username and pw? liubhs02 Linux - Newbie 2 06-01-2004 08:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:10 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