2

I have been having a problem identifying a flash drive in my code.

Luckily my code can be run from the flash drive. So is there a way in C (or C++) to tell what drive letter (or drive name) a program is running on?

Reason I need to know is when I plug the USB drive in, it is running a program that copies files from the computer to the USB drive itself.

John T
  • 23,735
  • 11
  • 56
  • 82
  • Which operating system are you developing on? – Kosi2801 Jul 10 '09 at 07:03
  • Why you don't use search? Very similar question (http://stackoverflow.com/questions/1105968/absolute-path-of-executable-start-directory) was asked just few hours ago. – qrdl Jul 10 '09 at 07:12
  • @qrdl - none of the terms in the 2 question titles overlap so OP could not have found that question without using completely different terminology. – danio Jul 10 '09 at 13:01

3 Answers3

4

GetModuleFileName can find out the driver letter for you, like this:

TCHAR ExeName[MAX_PATH];
GetModuleFileName(NULL, ExeName, MAX_PATH);
TCHAR DriveLetter = ExeName[0];

You might find the GetDriveType API useful as well.

RichieHindle
  • 272,464
  • 47
  • 358
  • 399
  • Note that this is not guaranteed to work properly. `GetModuleFileName` may return a short or long file name, and therefore may start with `\\?\` preceding the volume letter (or UNC reference). In that case, the first character would be "\". – Chris Krycho Aug 04 '15 at 20:41
1

You could use the ISO C++ _getcwd function to get the current working directory of your application like so:

#include <direct.h>
#include <stdio.h>

int main(int argc, char **argv)
{
    char buf[255];
    _getcwd(buf,255);
    printf("%c",buf[0]);
    return 0;
}

The char array buf will contain the path to your executable and buf[0] should supply you with just the letter of the drive.

John T
  • 23,735
  • 11
  • 56
  • 82
  • 1
    nitpick: _getcwd() is not ISO standard. getcwd() is part of POSIX; MS adds a '_' prefix because they don't consider POSIX standard enough to include in the CRT without the 'implementation-defined name' prefix. – Michael Burr Jul 13 '09 at 18:30
  • Also the current working directory will not necessarily be the directory where the application image lives (however depending on how the program should act, the current directory may be more appropriate to use than the application directory). – Michael Burr Jul 13 '09 at 18:32
  • It isn't standard it's conformant. I never used the word standard in my post. – John T Jul 13 '09 at 20:47
  • ISO is the International Organisation for Standards, so you did in fact refer to it, unintentionally perhaps. And the reason that Microsoft calls is "conformant" is because ISO C++ reserves _[a-z].* for implementation extensions. So _getcwd is a conformant extension of the standard, not part of ISO C++. – MSalters Jul 14 '09 at 11:05
-2

Somewhere on your C/C++ program you should have a main

like int main(int argc, char** argv)

the first argument of argv hold the path of your application

EDIT:

This is different between Windows and Linux. The following program output is different depending on the platform :

#include <iostream>

int main(int argc, char** argv) {

    std::cout << argv[0] << std::endl;
    return 0;
}

On Windows : (Visual Studio 2015)

C:\ConsoleApplication1\Debug\ConsoleApplication1.exe

On Linux : (Using online compiler with g++)

main

I would guess that the exact compiler used is irrelevant as it would be the OS job to provide those arguments.

Eric
  • 19,525
  • 19
  • 84
  • 147