C ++ paths starting with ~

Is it possible to use paths starting with "~" in C ++ codes on Linux? For example, this code does not work correctly:

#include <iostream>
#include <fstream>
using namespace std;

int main () 
{
  ofstream myfile;
  myfile.open ("~/example.txt");
  myfile << "Text in file .\n";
  myfile.close();
  return 0;
}
+4
source share
1 answer

I think you are on a Linux or POSIX system with an interactive shell that understands ~(for example, bash)

, ~, ( mkdir '~' , ). , globbing , ( !) ~, . /home/martin myprogram ~/example.txt . . glob (7). , glob (3) wordexp (3) ++ ( , "~/example.txt" - , - , ..)

getenv (3), ( getpwuid (3) getuid (2)). ,

std::string home=getenv("HOME");
std::string path= home+"/example.txt";
ofstream myfile(path);

, , getenv("HOME") NULL. .

. .

+5

Source: https://habr.com/ru/post/1614608/


All Articles