I use snprintfto write formatted data to disk, but I have one problem, how can I save it in the user's home directory?
snprintf
snprintf(filename, sizeof(filename), "%s_%s.sho", client, id);
On Linux and POSIX systems, the home directory is often found from an environment variable HOME. So you can encode
HOME
snprintf(filename, sizeof(filename), "%s/%s_%s.sho", getenv("HOME"), client, id);
Pedantically, getenv (3) may fail (or make a mistake). But this rarely happens. See environment (7) .
(You can check and / or use getpwuid (3) with getuid (2) ...)
setuid . , , ( ).
, HOME .
getuid() getpwuid() , :
getuid()
getpwuid()
#include <unistd.h> #include <pwd.h> struct passwd *pwd = getpwuid( getuid() ); /* need to duplicate the string - we don't know what pw_dir points to */ const char *homeDir = strdup( pwd->pw_dir );
...
getenv(3) HOME. , :
getenv(3)
#include <stdlib.h> #include <stdio.h> int main(void) { printf("%s\n", getenv("HOME")); return 0; }
filename , , .
filename
This should work on any Unix-like system, including Linux, macOS, BSD, and possibly many others.
Source: https://habr.com/ru/post/1677839/More articles:Two-player battle simulator stands for second player - javascriptUploading local images to React.js - imageHow to compress / delete lines with conditions in R? - vectorizationDoes the generic constructor of the inner class accept a type parameter that is set? - javaHow to call python from C ++ - c ++How to remove / remove Certbot Let Encrypt from Debian 8 - apacheHow to write a file using FileWriter for google dataproc? - java.net core / standard string.ToLower () does not have a culture parameter - c #All annotations are not displayed to the user in MKMapView - iosCreating a custom structural directive - angularAll Articles