Thread: Execute any DOS command through C program on VC++ compiler in windows2000professional

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    10

    Execute any DOS command through C program on VC++ compiler in windows2000professional

    hi all,

    I am beginner in C, I want to execute any DOS command like
    "cd <directory Name>"
    "dir"
    "cls" etc..... Through C program.

    Please any can help me .

    Thanz in advance.

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    You _Could_ do this, but there exists functions for EACH of these in several libs. . . Calling an external program from within your code is generaly considered to be "bad."

  3. #3
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Have you tried something like the system command?
    eg.

    system("dir");
    system("cd"); etc...

    http://www.thinkage.ca/english/gcos/...ib/system.html

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    As stated, you could use the system() command but that's sort of OS dependant.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    system() can also be dangerous. Imagine if someone mapped "cls" to "bad_trojan-2.5.exe" on some computer.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Hence how it is considered to be "bad."

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Aside from that, system("cd foo"); does NOT change the directory of the program itself, much to the surprise of newbies.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The POSIX function chdir() does, however.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Didn't we kill the DOS board?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DOS Program
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 02-12-2002, 08:55 PM
  2. Program to run in Dos only
    By ihsir in forum C++ Programming
    Replies: 2
    Last Post: 01-19-2002, 06:16 PM
  3. DOS program versus DOS console program
    By scromer in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-10-2002, 01:42 PM
  4. Replies: 2
    Last Post: 12-31-2001, 08:23 PM
  5. Replies: 7
    Last Post: 09-07-2001, 11:10 AM