get user?

Pages: 12
for my program i need to use chdir() to locate to a folder. right now i ust have it as chdir("/home/ty/ncursesgame/files"); and id rather have it look something likechdir("/home/USER/ncursesgame/files"); where it would autofill the user in for the user of that computer or something. can anyone help out? basically it needs to just know whoever the current user is and use that
the first link didnt work for what i wanted. the second one i didnt know what it was taking about. here the code that i need the paths to be changed if anyone can just straight do it for me. thanks!

1
2
3
4
5
6
7
8
9
10
11
#include <curses.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <term.h>
int main()
{ 
chdir("/home/ty/ncursesgame/files");
system( "gcc -o ncursesv6 ncursesv6.cpp -lncurses");
system( "gnome-terminal -e ./ncursesv6" );
}


and a snippet of the second one...

1
2
3
4
5
6
7
8
      if (wy==2) {
        #include "new.cpp"
        refresh();
        scr_restore("/home/ty/ncursesgame/files/world.dump");
        doupdate();
        refresh();
        wy = 0;
      }
1
2
3
4
5
6
7
8
9
10
11
#include <cstdlib>
#include <string>
#include <iostream>

int main(){
   const char *home = getenv("HOME");
   if(home)
      std::cout << home << "/.local/share/some_game/" << std::endl;
   else
      std::cerr << "HOME variable not set\n";
}
ne555 i am using ncurses. is this possible without iostream?

ne555 heres what i tried. i t didnt work:
1
2
3
4
5
6
7
8
9
10
11
12
#include <curses.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <term.h>
int main()
{ 
getenv("HOME");
chdir("/ncursesgame/files");
system( "gcc -o ncursesv6 ncursesv6.cpp -lncurses");
system( "gnome-terminal -e ./ncursesv6" );
}

i used to have it without the getenv("HOME"); and just had chdir("/ncursesgame/files"); as chdir("/home/ty/ncursesgame/files"); and it worked then
Last edited on
If you don't catch the result value, then it's useless.
1
2
3
4
const char *home = getenv("HOME");
if( not home ) return 1;
std::string directory = std::string(home) + std::string("/ncurses/files");
chdir( directory.c_str() );
I'm not sure gopro2027 understands what getenv() really does and how he could get the $USER or $HOME environment value.

if you want your following code:

chdir("/home/USER/ncursesgame/files");

to have USER filled with the actual user
you have to fetch the current environment's variable first:

1
2
const char *user = getenv("USER");
const char *home = getenv("HOME");


and then you can use this variable to fill the part you wanted.

Hint: String operations can be pretty handy.
Last edited on
its still not working. i did
1
2
3
const char *user = getenv("USER");
const char *home = getenv("HOME");
chdir("/home/USER/ncursesgame/files");

and i tried some other things too and it didnt work
I know! But you have to find a way to reach the "correct" path.

Try to print on any "output" the variables: user and home

You will then understand that they contain the $USER's name and the $HOME's path respectively...

Usually the $HOME contains: /home/USER

Now you have to figure out a way to 'concatenate' the $HOME with the correct path to make
1
2
3
4
// as a std::string
chdir(gamefiles_path.c_str());
// OR as a char *
chdir(gamefiles_path)

work correctly.

Hint:
1
2
std::string gamefiles_path = home // + ???
// char * gamefiles_path = home + // ?? 
Last edited on
heres what i got. it still isnt working. i think i need to somehow have gamefiles as a varriable not a part of it
1
2
3
const char *home = getenv("HOME");
const char *gamefiles = home;
chdir("/gamefiles/ncursesgame/files");
Heh, you don't get it...

Try this line of code:

1
2
3
4
std::string home = getenv("HOME"); // To recover the user's home directory
std::string gamefiles = home + "/ncursesgame/files"; // because you want to add the extentsion

chdir(gamefiles.c_str());


Because you might not have a environment you should ALWAYS check the return value of getenv();
Last edited on
i was very close to that. i was thinking that i would need make gamefiles = to home + the ncursesgame/files but now i know i was missing out on the quotation marks cuz i kept getting error "ncursesgame not declared" and "files not declared"
Yes, veeeeeeeeery close it was almost frustrating :)
quick question, is it only possible with strings? i cant get it to work like this and i think it should be right:
1
2
3
const char *home = getenv("HOME");
const char *gamefiles = home + "/ncursesgame/files";
chdir(gamefiles);

it gives me an error about the + sign
Last edited on
It gives you an error, because the operator "+" is not used/overloaded for "const char *" types.

You have 3 ways of solving this :)

1: You duplicate the arrays
2: You pass by a std::string to recover it
3: You overload the + operator

I would suggest the second one (since we're in C++ :D):
const char *gamefiles = std::string(home + "/ncursesgame/files").c_str();
Last edited on
thanks! and one other thing. its kinda nooby, but i forgot what header to put for strings... :P
well it might now be the header. i put #include <string> and that should work but its not.
Last edited on
wait do i need iostream for a string? cuz im using ncurses and i cant have iostream
1
2
3
4
#include <string>  // for std::string

#include <string.h> // c lib for string operations
#include <cstring> // same as above in C++ syntax 

You don't need iostream
phew i finally got it to work. i had to take out ncurses.h (as this is only the file for opening the terminal and locating to the folder with the ncurses file) and put in iostream and string.h and then instead of using my usual commpile code, i had to jut simplify it to "make STARTGAME" :) lol

so to sum it up, the answer to this question is:
1
2
3
4
5
6
7
8
//headers
#include <iostream>
#include <string.h>

//the three lines to locate to the folder
std::string home = getenv("HOME"); 
std::string gamefiles = home + "/ncursesgame/files";
chdir(gamefiles.c_str());

thanks SeiZa! now i can upload the first stable version of my game

EDIT: i guess u dont need iostream :P
Last edited on
Pages: 12