Hey. How can I make my C program get the drive letter? Like "C:".
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?
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
I think he means "/dev/hda1"
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
*shakes head*
if you searched for the answer AT ALL you would find it... www.google.com
i seem to have GCC 3.3.4
But how do i start it?
I dont have a menu for it or anything.
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: driveCode:You are using the u: drive.
Yes, his method won't work if executed using the command prompt.
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
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; }
Last edited by master5001; 12-19-2004 at 11:33 AM.
>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.
My best code is written with the delete key.
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; }
Last edited by master5001; 12-19-2004 at 11:32 AM.
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.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
for windows machines you can use getdisk() from dir.h.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
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; }