DOS Command

This is a discussion on DOS Command within the C++ Programming forums, part of the General Programming Boards category; I want to perform the DOS command 'DIR' within a C++ program. The results to be available to the C++ ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    1

    DOS Command

    I want to perform the DOS command 'DIR' within a C++ program. The results to be available to the C++ program also. How do I???

  2. #2
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    I don't really know but i think you are talking about

    Code:
    system("dir");

  3. #3
    Alipha
    Guest
    You need to redirect the output to a file, then read the file. (I dunno how to read a file though, so couldn't help you there)

    system("dir > dir.txt");

    Now you can open dir.txt.

  4. #4
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163

    like so...

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    
    void read_file ();
    
    int main()
    {
    char yesno;
    
    cout<<"Display conents of directory [y/n]"<<endl;
    
    yesno = getch();
    
    switch(yesno)
    {
    case 'n':
    cout<<"Ok, Exiting..."<<endl;
    return 0;
    break;
    case 'y':
    cout<<"Loading..."<<endl;
    system("dir > dir.txt");
    read_file();
    break;
    default:
    cout<<"Not an option..."<<endl;
    return 0;
    break;
    }
    return 0;
    }
    
    void read_file()
    {
    char content[1000];
    
    ifstream load;
    load.open("dir.txt");
    load.read(content, 1000);
    load.close();
    
    cout<<content<<endl;
    cout<<"Closeing..."<<endl;
    system("pause");
    }
    You might have to clean it up before it compiles but that should work...
    +++
    ++
    + Sekti
    ++
    +++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 11:43 AM
  2. Can I issue a command to DOS from within a program?
    By Sukarn in forum Windows Programming
    Replies: 2
    Last Post: 12-22-2006, 07:44 AM
  3. Executing DOS command in C
    By m.sudhakar in forum C Programming
    Replies: 3
    Last Post: 07-22-2006, 03:52 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM
  5. Command Line Variable Passing (like in DOS)
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-31-2002, 08:31 AM

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