How do I get the Drive Letter?

This is a discussion on How do I get the Drive Letter? within the C++ Programming forums, part of the General Programming Boards category; Hey. How can I make my C program get the drive letter? Like "C:"....

  1. #1
    LiX
    LiX is offline
    Registered User
    Join Date
    Nov 2004
    Posts
    3

    Exclamation How do I get the Drive Letter?

    Hey. How can I make my C program get the drive letter? Like "C:".

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    *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.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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;
    }

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    The output to the above program was
    Code:
    You are using the u: drive.
    I ran it from the d: drive

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    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

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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 10:33 AM.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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
    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;
    }
    Which could avoid possible buffer overflow issues. It wouldn't hurt to do error checking, but I'll leave that up to you.
    Last edited by master5001; 12-19-2004 at 10:32 AM.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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;
    }

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help using strings and mapping
    By trprince in forum C Programming
    Replies: 29
    Last Post: 12-01-2007, 03:01 PM
  2. Drive letter
    By opafire in forum C++ Programming
    Replies: 8
    Last Post: 04-22-2004, 08:02 PM
  3. Choosing drive letter
    By *ClownPimp* in forum Tech Board
    Replies: 4
    Last Post: 07-11-2003, 02:40 PM
  4. probe for drive letter assignment
    By iain in forum C Programming
    Replies: 3
    Last Post: 02-07-2002, 07:49 PM
  5. Drive letter
    By CyberCreationz in forum C Programming
    Replies: 1
    Last Post: 02-07-2002, 07:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21