• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

string substitution


  char* usr = getlogin() ;
  ifstream in("/var/mail/   <output here>  ") ;

I want to substitute the output of getlogin() in the above ifstream, how shoud i, helps please
--begin.
0
begin
Asked:
begin
1 Solution
 
KangaRooCommented:
char* usr = getlogin();
char* basepath="/var/mail";
char* filename = new char[strlen(usr)+strlen(basepath)+1];
strcpy(filename, basepath);
strcat(filename, usr);
ifstream in(filename);

// === or ===
std::string usr(getlogin());
ifstream in((std::string("/var/mail/") + usr).c_str())
0
 
nietodCommented:
I strongly recommend the 2nd approach, but would change it to

std::string FilNam  = "/var/mail/";
FilNam += getlogin();
ifstream in(FilNam.c_str());

for readability.
0
 
beginAuthor Commented:
thanks for your solutions.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now