user3536228
user3536228

Reputation: 73

Accessing %appdata% with c

I want to make a program that open a text file present in the startup folder.Write something to it and close it. Can i use %APPDATA% in my path because user name is changed at every pc I used as below but its not working.

FILE *fptr
fptr = fopen("%APPDATA%\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\myfile.txt","w");

Upvotes: 1

Views: 1075

Answers (1)

Sergey L.
Sergey L.

Reputation: 22542

The regular way to get environment variables is to use getenv

char * appdata = getenv("APPDATA");
if (!appdata) { /* error */ }
char buffer[0x400];
snprintf(buffer, sizeof(buffer)
    , "%s\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\myfile.txt", appdata);
fptr = fopen(buffer,"w");

Please keep in mind that in windows environment variables are not case sensitive.

Upvotes: 1

Related Questions