1

How can I create a file/folder in Windows using a C program under a given user?

For example, if I have to make something in Desktop "C:\Users(user)\Desktop"

I'm using a system call in C, so I have to substitute the user for the actual username, how can I do that?

int b = 1; char mkdir[33] = "MKDIR Desktop\\"; sprintf(mkdir, "MKDIR Desktop\\%i", b); system(mkdir);

But instead of creating a file at Desktop it continues in the current directory and creates a file called Desktop?

sourabh1024
  • 647
  • 5
  • 15
  • See if this helps https://stackoverflow.com/questions/23271990/how-to-create-a-folder-in-c-need-to-run-on-both-linux-and-windows/23272187#23272187 – sourabh1024 Dec 30 '20 at 18:46

1 Answers1

0

What can I make if I have to go to a directory within one of the users in C?

The C language standard (read Modern C then e.g. n2126) does not know about directories, so you need to use some operating system specific API. See also this C reference website.

For Linux (or POSIX), read Advanced Linux Programming then about syscalls(2) and use mkdir(2)

For Windows, read more about the Windows API and the directory management.

You might use system with some MKDIR operating system specific command, but that is less efficient (and you still need to handle failure).

Take inspiration from existing open source code.

(e.g. GNU findutils)

You could use cross-platform opensource libraries like GTK and Glib.

   sprintf(mkdir, "MKDIR Desktop\\%i", b); 

Your use of sprintf is dangerous because of potential buffer overflow. Consider using snprintf.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547