opening file without drive letter

This is a discussion on opening file without drive letter within the Windows Programming forums, part of the Platform Specific Boards category; When opening a file without drive letter in the path name takes windows the drive from the working directory of ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    opening file without drive letter

    When opening a file without drive letter in the path name takes windows the drive from the working directory of the program or what does it do?

  2. #2
    Registered User
    Join Date
    Dec 2011
    Location
    Philippines
    Posts
    15
    do you mean file handling ?

  3. #3
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    963
    When you run a program, you run it in the context of a working directory. So if you open a file without a drive letter (relative path), it will be in relation to the current working directory. It may also search PATH, but I can't say for sure on that and wouldn't count on that.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by KIBO View Post
    When opening a file without drive letter in the path name takes windows the drive from the working directory of the program or what does it do?
    Epy is very close...

    Your working directory ( GetCurrentDirectory() and SetCurrentDirectory() ) is considered to be your "home" directory... Relative paths are deemed to originate from there. It works like this...

    E:\myfiles\this\that\private
    This is an explicit path, it leads directly to the last folder in the chain (private, in this example)

    \myfiles\this\that\private
    This is a root path... windows will substitue in the current drive and expand it to an explicit path like this...
    D:\myfiles\this\that\private

    myfiles\this\that\private
    This is a relative path ... windows will concatenate it onto the end of your current directory, like this...
    c:\projects\coding\myfiles\this\that\private

    In the second two examples, it is very likely the path won't exist and windows will not create it for you automatically...

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    A Windows process in a sense has multiple current directories, one for each volume. For instance, if you set the current directory to "D:\foo", then set it to "C:\windows", then set it to "D:", you end up in "D:\foo", not the root of D:

    There is only one actual "working directory" but the last working directory of each volume is memorized.

    You can test this from a command prompt.

    Code:
    C:\> d:
    D:\> cd foo
    D:\foo> c:
    C:\> dir d:
    [[[ the contents of d:\foo will be listed]]]
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    Quote Originally Posted by brewbuck View Post
    A Windows process in a sense has multiple current directories, one for each volume.
    That truthfulness of that depends on how picky you want to be. At the plain Win32 level there's only ever one current directory and nothing is remembered. cmd hacks it for back compat using weird environment variables to save them.

    If you use _chdir from the MS CRT to change directories, that will also create these environment variables. They're looked up and returned if you call GetFullPathName("X:" ...), despite that being deeper in the OS where there is no such concept.

    Code:
    #include <windows.h>
    #include <direct.h>
    #include <stdio.h>
    
    int main()
    {
        WCHAR buffer[MAX_PATH];
        SetCurrentDirectoryW(L"C:\\test");
        SetCurrentDirectoryW(L"D:\\");
        GetFullPathNameW(L"C:", MAX_PATH, buffer, NULL);
        // this will print C:\ or whatever the current C:\ directory
        // was in cmd if you launch it from there
        printf("%S\n", buffer);
        _chdir("C:\\Test");
        _chdir("D:\\");
        GetFullPathNameW(L"C:", MAX_PATH, buffer, NULL);
       // will print C:\test
        printf("%S\n", buffer);
        return 0;
    }
    Not that anybody in this thread whose username doesn't start with an a cares about this, or that it affects the OP in any way
    Last edited by adeyblue; 12-20-2011 at 06:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening a file from hard drive
    By joeman in forum C++ Programming
    Replies: 6
    Last Post: 03-04-2010, 07:09 PM
  2. How do I get the Drive Letter?
    By LiX in forum C++ Programming
    Replies: 12
    Last Post: 12-19-2004, 10:48 AM
  3. Drive letter
    By opafire in forum C++ Programming
    Replies: 8
    Last Post: 04-22-2004, 08:02 PM
  4. drive letter
    By RoD in forum Tech Board
    Replies: 5
    Last Post: 03-27-2004, 06:45 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