Hey. How can I make my C program get the drive letter? Like "C:".
Printable View
Hey. How can I make my C program get the drive letter? Like "C:".
const char *dl = "C:";
What drive letter?
And why are you asking a Windows-specific question about a C program in the C++ forum?
I think he means "/dev/hda1"
*shakes head*
if you searched for the answer AT ALL you would find it... www.google.com
There are better ways of doing it, but I've become demotivated to post on the boards so I'll keep it short:
Example
Code:#include <iostream>
int main(int argc, char **argv) {
char drive = *argv[0];
std::cout << "You are using the " << drive << ": drive." << std::endl;
return 0;
}
The output to the above program was
I ran it from the d: drive :pCode:You are using the u: drive.
Yes, his method won't work if executed using the command prompt.
Yeah I suspected that would happen. There are a couple windows specific ways to do this, but since the question is inherently windows specific I don't think thats a problem :)
GetCurrentDirectory() should do the trick for this sort of program. Then grab first letter out of the buffer you passed into GetCurrentDirectory().
Example:
Code:#include <windows.h>
#include <iostream>
int main(void) {
char buffer[MAX_PATH], drive;
GetCurrentDirectory(MAX_PATH, buffer);
drive = *buffer;
std::cout << "You are using the " << drive << ": drive." << std::endl;
return 0;
}
>char drive = *argv[0];
Be sure to check that argc is greater than 0, otherwise you're dereferencing a null pointer. Also don't forget to take into account that argv[0] (if present) isn't required to have the complete path, or even a relative path. It can be an empty string. Since this operation is inherently operating system dependent, your second solution is far better.
What she said is more or less why I figured you may end up with an invalid path (or potential crash). The second way gets the work directory for your program. You may want to check the return of the function to make sure the function didn't fail (it will return 0 upon failure). When doing operations that work with strings such as the above where a string buffer will be passed into a function that is supposed to alter the buffer, but won't if failure occurs, its a good practice to add this to your code:
Example
Which could avoid possible buffer overflow issues. It wouldn't hurt to do error checking, but I'll leave that up to you.Code:#include <windows.h>
#include <iostream>
int main(void) {
char buffer[MAX_PATH], drive;
*buffer = 0;
GetCurrentDirectory(MAX_PATH, buffer);
drive = *buffer;
std::cout << "You are using the " << drive << ": drive." << std::endl;
return 0;
}
And of course that still won't work, because even on Windows, not every path, not even every absolute path, starts with a drive letter. UNC paths don't. That usually means paths to network shares.
for windows machines you can use getdisk() from dir.h.
Hmmm whats an easy work around for the network path thing... I guess something like this:
(btw, on earlier examples I guess I had the params on GetCurrentDirectory() swapped. I never used it and its unusual for the buffer size parameter to come before the actual buffer...)
Example:
Code:#include <windows.h>
#include <iostream>
int main(void) {
char buffer[MAX_PATH], drive;
*buffer = 0;
if(GetCurrentDirectory(MAX_PATH, buffer)) {
switch(GetDriveType(NULL)) {
case DRIVE_UNKNOWN:
std::cout << "Invalid drive." << std::endl;
break;
case DRIVE_NO_ROOT_DIR:
std::cout << "Inavalid root path." << std::endl;
break;
case DRIVE_REMOTE:
std::cout << "Network drive." << std::endl;
break;
case DRIVE_REMOVABLE:
case DRIVE_FIXED:
case DRIVE_CDROM:
drive = *buffer;
std::cout << "You are using the " << drive << ": drive." << std::endl;
break;
case DRIVE_RAMDISK:
std::cout << "RAM Disk Drive." << std::endl;
break;
}
}
return 0;
}