KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q41025: Using _dos_setdrive and _searchenv to Open a File

Article: Q41025
Product(s): See article
Version(s): 2.00
Operating System(s): MS-DOS
Keyword(s): ENDUSER | | mspl13_c
Last Modified: 28-FEB-1989

Question:

How can I open a file on a floppy drive without explicitly specifying
a full path in the fopen statement?

Response:

You can use the run-time function _dos_setdrive to set the current
drive to floppy Drive A. Then use the function _searchenv to search
for the filename on a floppy disk in Drive A. The full path to the
specified file will be copied to the buffer. Then you can use fopen to
open the file specified by the buffer.

Note: _searchenv works properly using QuickC Version 2.00. This
program will not work correctly under the earlier versions of QuickC
or C Version 5.10.

The following is a sample code:

#include <dos.h>
#include <stdio.h>
#include <stdlib.h>

main()
{
    FILE *stream;
    unsigned drive;
    char buffer[40];

    /* set default drive to be drive A */
    _dos_setdrive (1, &drive);
    _dos_getdrive (&drive);
    printf( "Current drive: %c:\n", 'A' + drive - 1 );

    /* Search for file at root level */
    _searchenv("data","",buffer);
    printf("path: %s\n", buffer);

    if ((stream = fopen(buffer,"r")) == NULL)
       printf ("Could not open file\n");
    else
       printf("File opened for reading\n");
}

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.