Reputation: 13
Here is the code I am working on. What changes are required for the program to automatically detect its exe file path and save it in a string variable?
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<dir.h>
void main()
{
int check;
char dirname[150], u_name[30];
printf("Enter a username:");
scanf("%s",&u_name);
strcpy(dirname,"C:/Users/Bilal/Desktop/");
strcat(dirname,u_name);
check = mkdir(dirname);
if (!check)
printf("Directory created\n");
else
{
printf("Unable to create directory\n");
exit(1);
}
printf("\nPress any key to exit program");
getch();
}
Upvotes: 0
Views: 141
Reputation: 6467
GetModuleFileName will tell you where exe is running from.
#include <windows.h>
...
WCHAR dirname[1024];
GetModuleFileNameW(NULL, dirname, 1024);
Convert wchar_t
to char
using wcstombs.
strcat(dirname, whatever); // Or strncat
mkdir(dirname);
Upvotes: 2