1

I have this file that I'm making as a fun personal project that creates a bunch of files called yeet from a short choice of directories

the issue is somewhere between the directory array and the file opening so that's what I'll show here

the array looks like this

char patharrays[13][30] = {
            "downloads/",
            "documents/",
            "videos/",
            "pictures/",
            "favorites/",
            "music/",
            "onedrive/",
            "links/",
            "contacts/",
            "searches/",
            "\"Saved Games\"/",
            "../../\"Program Files\"/",
            "../../\"Program Files (x86)\"/"
        };
        int randomPathNum = random_number(0, 13);

while the file creator loop looks like this ( y is a random number between 1 and 1000 )

for (int x = 1; x <= y; x++) {
                FILE *yeetfile;
                char filename[58];
                sprintf(filename, "%%USERPROFILE%%/%syeet%i.txt", patharrays[randomPathNum], x);
                yeetfile = fopen(filename, "w");
                fputs("lol, imagine getting yeeted", yeetfile);
                fclose(yeetfile);
            }

I started doing C two days ago and I'm not quite sure on how to approach this, any help would be amazing.

Note: When using hard paths that use my actual profile name, instead of %USERPROFILE%, the code works and creates the files. It fails most likely due to my confusion with %

  • 6
    %USERPROFILE% is an environment variable and cannot be opened in a path as-is. You will need to use `getenv()` to retrieve the actual path of %USERPROFILE%. – xnsc Mar 30 '22 at 05:13
  • Does this answer your question? [expand file names that have environment variables in their path](https://stackoverflow.com/questions/1902681/expand-file-names-that-have-environment-variables-in-their-path) – phuclv Mar 31 '22 at 02:24
  • duplicates: [How to use or expand environment variables in a command instantiated by CreateProcess?](https://stackoverflow.com/q/9890746/995714), [Using fopen with temp system variable](https://stackoverflow.com/q/29795648/995714), [Using Windows Environment Variable in Native Code](https://stackoverflow.com/q/4788983/995714), [expand file names that have environment variables in their path](https://stackoverflow.com/q/1902681/995714) – phuclv Mar 31 '22 at 02:29

1 Answers1

2

the getenv() does exactly what I was looking for

sprintf(filename, "%s/%syeet%i.txt", getenv("USERPROFILE"), patharrays[randomPathNum], x);
  • no it isn't quite correct. You need to expand the environment variable in the path with [`ExpandEnvironmentStrings`](https://learn.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-expandenvironmentstringsw) – phuclv Mar 31 '22 at 02:27