Home > database >  Check if a file exist using a environment variable and print the result with C
Check if a file exist using a environment variable and print the result with C

Time:11-14

How can I check if a file exists using a environment variable %APPDATA% or something similar with C ? and if the file was found must print "File Found" or if not found must print "File not Found"

#include <stdio.h>
#include <unistd.h> 
#include <windows.h>

int main(void)
{
    if (access("%APPDATA%\\changzhi_leidianmac.data", F_OK) != -1)
    {
        printf("File Found......");
    }
    else
    {
        printf("File not found !!!!");
    }

    return 0;
}

CodePudding user response:

You can use getenv

#include <stdlib.h>

int main(void)
{
    char *appdata = getenv("APPDATA");
    ...
    ...
}

CodePudding user response:

C does not have any provisions for accessing env variables. You need to use either a library or system commands to get the data.

  • Related