Thread: system()

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    36

    Lightbulb system()

    The function system() can be used for executing a DOS Command or Application from inside an executing program.

    Exaple:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void main() {
    
      printf("Listing the Directory Contents:\n");
      
      system("dir");
    
      printf("\nPress any key to exit from the program...");  
      getch();
    
      exit(0);
    
    }
    The above example program does display the output of the printf() function but the code line:

    Code:
    system("dir");
    doesn't work.


    Any help?

  2. #2
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void main() {
      FILE *dir = popen("dir", "r");
      char line[500];
    
      printf("Listing the Directory Contents:\n");
    
      while(fgets(line, sizeof line, dir))
        puts(line);
    
      pclose(dir);
    
      printf("\nPress any key to exit from the program...");  
      getch();
    
      return 0;
    }
    Happy?

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    36

    Displaying the contents of a text file

    Hi,


    Thanks for your post but it's not what I wanted to do. I want to display the contents of a folder containg files and folders by running the DIR command of DOS.

    However, your program's modified version is here as the original one would not run:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    void main() {
    
      FILE *dir = fopen("dir.txt", "r");
      char line[500];
    
      printf("Listing the Directory Contents:\n");
    
      while(fgets(line, sizeof line, dir))
        puts(line);
    
      fclose(dir);
    
      printf("\nPress any key to exit from the program...");
      getch();
    
    }


    The above program reads and displays the contents of a named file which is here in our example is "dir.txt".

    Quote Originally Posted by c++0x View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void main() {
      FILE *dir = popen("dir", "r");
      char line[500];
    
      printf("Listing the Directory Contents:\n");
    
      while(fgets(line, sizeof line, dir))
        puts(line);
    
      pclose(dir);
    
      printf("\nPress any key to exit from the program...");  
      getch();
    
      return 0;
    }
    Happy?

  4. #4
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    But you don't want to open a file, you want to open a process. What was popen doing?

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    I don't know which environnement (C runtime) you are using, but look for the documentation on the system function call. Then, check the return value of the system call. Because I don't see why your "system("dir")" call would fail on a normal Windows system.
    I hate real numbers.

  6. #6
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    Or POSIX. Nor do I c y my popen code not work.

  7. #7
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    
    int main(void)
    {
      FILE *dir = popen("dir", "r");
      char line[500];
    
      assert(dir != NULL);
    
      printf("Listing the Directory Contents:\n");
    
      while(fgets(line, sizeof line, dir))
      {
        puts(line);
        fflush(stdout);
      }
    
      pclose(dir);
    
      printf("\nPress any key to exit from the program...");  
      getch();
    
      return 0;
    }
    Mayb? I don no y my popen not workd. Mayb cuz I cpy ur void main() lol. I fix it tho.
    Last edited by c++0x; 12-10-2008 at 08:21 PM.

  8. #8
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    Wait! If dir not work with system. I bet u dat ur PATH variable is buggred up. Fix ur PATH environment variable. If dats da case, popen won't work eithr. It will just crash n burn.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The problem is that "dir" is not the name of any program. It is an internal command delegate of cmd.exe. You need:

    Code:
    system("cmd /c dir");
    EDIT: if you don't understand what's going on here, you shouldn't be doing this.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Wait! If dir not work with system. I bet u dat ur PATH variable is buggred up. Fix ur PATH environment variable. If dats da case, popen won't work eithr. It will just crash n burn.
    http://www.catb.org/~esr/faqs/smart-...html#writewell

    I mean your first post was quite sane, but now your speech is just an open sewer.
    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.

  11. #11
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    Quote Originally Posted by devarishi View Post
    The function system() can be used for executing a DOS Command or Application from inside an executing program.
    On Solaris too?

  12. #12
    Registered User
    Join Date
    Dec 2008
    Posts
    36
    Hi,


    "Nonportable pointer conversion" warning message is being flagged highlighting the code line:

    Code:
    FILE *dir = popen("dir", "r");

    Just now I have tried it again and this time included dos.h and process.h header files also. But the same error message is coming there.

    I corrected the function main() also.

    Quote Originally Posted by c++0x View Post
    But you don't want to open a file, you want to open a process. What was popen doing?

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably don't have popen() - try _popen() if you are using visual studio or gcc-mingw.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Dec 2008
    Posts
    36
    Well, it is also not working. The same error message, as given above, is there.
    Quote Originally Posted by c++0x View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    
    int main(void)
    {
      FILE *dir = popen("dir", "r");
      char line[500];
    
      assert(dir != NULL);
    
      printf("Listing the Directory Contents:\n");
    
      while(fgets(line, sizeof line, dir))
      {
        puts(line);
        fflush(stdout);
      }
    
      pclose(dir);
    
      printf("\nPress any key to exit from the program...");  
      getch();
    
      return 0;
    }
    Mayb? I don no y my popen not workd. Mayb cuz I cpy ur void main() lol. I fix it tho.

  15. #15
    Registered User
    Join Date
    Dec 2008
    Posts
    36
    The given below code doesn't display anything on the console/screen.

    Code:
    system("cmd /c dir");

    Well, my Path is also correct. I am using TC & TCPP (Turbo C++ ) compilers. However, now I have installed VC++ 2005 Express Edition also.

    Quote Originally Posted by brewbuck View Post
    The problem is that "dir" is not the name of any program. It is an internal command delegate of cmd.exe. You need:

    Code:
    system("cmd /c dir");
    EDIT: if you don't understand what's going on here, you shouldn't be doing this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File System Implementation
    By dodgeviper in forum C Programming
    Replies: 9
    Last Post: 11-16-2007, 01:04 PM
  2. Using system icons
    By @nthony in forum Windows Programming
    Replies: 1
    Last Post: 01-13-2007, 07:56 PM
  3. Linux database system needed
    By BobS0327 in forum Tech Board
    Replies: 7
    Last Post: 06-11-2006, 03:56 PM
  4. BIOS system and memory allocation problem
    By beely in forum Tech Board
    Replies: 9
    Last Post: 11-25-2003, 07:12 AM
  5. Number system base M, print numbers N digits wide...
    By biterman in forum C Programming
    Replies: 12
    Last Post: 11-19-2001, 04:31 AM