0

I want to do a file operation and write file to the folder which is something in $(USER) directory.

    // static const char* out_filename = "/data/panda/output/out.yuv"; //NO ERROR 
    static const char* out_filename = "/data/$(USER)/output/out.yuv"; // ERROR

    FILE* fs_dmp= fopen(out_filename, "w+");
    if (NULL == fs_dmp) {
        printf("ERROR creating output file %s\n", out_filename);
        exit(1);
    }

When I am running the c code,

Error: ERROR creating output file /data/output/out.yuv

UPDATE0:

After @Fred Larson suggestion, I tried to use getenv() code to get the USER name (environment variable) but my goal is to use this USER name and create a username folder in /data/$(USER) this way.

UPDATE1:

I tried using sprintf this way :

const char *env_p = getenv(name);
if (env_p) {
    printf("%s = %s\n", name, env_p);
}

char out_sprint_filename[100];
int n = sprintf(out_sprint_filename, "/data/%s/output/out.yuv", env_p);
printf ("Name of file: %s \n", out_sprint_filename); 
Danny
  • 68
  • 6
  • Hint: Look at [`getenv()`](https://en.cppreference.com/w/c/program/getenv) – Fred Larson Feb 28 '22 at 16:26
  • Does this answer your question? [How to use environment variable in a C program](https://stackoverflow.com/questions/31906192/how-to-use-environment-variable-in-a-c-program) – Fred Larson Feb 28 '22 at 16:29
  • @FredLarson, this helps me just get the name of the USER. But how can I add this in file name so file will write in perticular ’$USER/output’ folder – Danny Feb 28 '22 at 16:36
  • @FredLarson, link is explaining environment variable location.I want to use that variable in other variables path name. For example here /data/$(USER)/output/out.yuv – Danny Feb 28 '22 at 16:42
  • 1
    [`sprintf()`](https://en.cppreference.com/w/c/io/fprintf) might be a good choice, but make sure you have enough space in your destination buffer. – Fred Larson Feb 28 '22 at 16:50
  • my destination is on server. So that wont be an issue. – Danny Feb 28 '22 at 16:52
  • 1
    @Danny, "destination buffer" refers to the space in your program's memory where you would construct the wanted file/directory name. That has very little to do with the type or capabilities of the machine on which the file will reside. – John Bollinger Feb 28 '22 at 16:58
  • I get the feeling that you are a bit out of your depth here. I'm uncertain whether that is specifically to do with C or with programming more generally. Is there another viable language with which you would be more comfortable? Python, maybe, or Bash? – John Bollinger Feb 28 '22 at 17:01
  • @FredLarson, sprintf help me to get the required file name. – Danny Feb 28 '22 at 17:13
  • @JohnBollinger, did not get what you want to say. – Danny Feb 28 '22 at 17:14

0 Answers0