#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Brussels
    Posts
    3
    Rep Power
    0

    I canít launch Windows files from inside my c++ program.


    I have a file on c:\2.txt
    Everything is OK with my command line
    C:\>2.txt
    I can also compile my file ex1.cpp :
    #include <stdio.h>
    #include <dos.h>
    #include <stdlib.h>
    main(){

    system("2.txt");

    }

    C:\>bcc ex1.cpp //OK I have got ex1.exe

    But when I try to do

    C:\>ex1

    I have a massage
    Bad command or file name.

    There is no problem when I do the same thing with a file ex2.cpp
    #include <stdio.h>
    #include <dos.h>
    #include <stdlib.h>
    main(){

    system("time");

    }
    C:\>bcc ex2.cpp //OK I have got ex2.exe
    C:\>ex2 // No problem and I can read a time


    My question is: Why I can launch any file from my Windowsís programs using directly command line
    ( like this:
    C:\>Bel.wav or C:\>2.txt and it causes launching the corresponded program without any problem)
    But I canít launch Windows files from inside my c++ program.
    What can help me here?
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Apparently you did not understand the previous two answers to this same question.

    On DOS and even in Windows, in order to launch a program the OS needs to know the name and location of that program. Then in addition, it needs to provide that program with any arguments that it needs.

    In DOS, this is handled by the basic command syntax:
    command [argument list]
    That command can be either an "internal command" or an "external command". Internal commands are handled by the shell, command.com or cmd.exe, itself and are of no interest in this discussion.

    External commands are executable program files that the shell needs to locate and load and start executing. The location of a program can either be entered explicitly, eg c:\utils\grep.exe, or it can be placed into one of the directories listed in the PATH environment variable (type the command path to display that list.

    But still, the bottom line is that the name of the program must be given to the OS or else it will never by executed. Trying to give the OS the name of a non-program file as a command will not work! That is why your entry of "commands" like 2.txt name is met with "Bad command or file name". 2.txt is not an executable program file.

    Now you are asking, "Well, in Windows I can double-click on the 2.txt icon and it opens up Notepad! Why does it work there?" OK, open Windows Explorer and open Folder Options under Tools. Then click on the File Types tab. There you will see listed the registered file types. Then click on any file type and you will see that Windows associates all files having that particular extension with a particular executable program. In particular, click on the TXT extension and you should see in the details box that it has been associated with Notepad. So when you double-click on 2.txt, Windows checks that association and sees that it needs to launch Notepad. That is a Windows operation, not DOS.

    So in your calls to system(), you need to provide that association between the file type and the program that will use that file. Either you need to require the program name to be entered explicitly, or you need to create your own lookup table associating certain extensions with certain programs, or you need to get into the registry to discover what associations Windows has already set.

    In that last case, you will need to ask somebody else how to read the registry.

IMN logo majestic logo threadwatch logo seochat tools logo