Thread: why system () command not work in GCC ?

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    3

    why system () command not work in GCC ?

    I have Win XP. When I open the DOS prompt using "cmd", and then,
    from the DOS command line, I type a command like this, it works:

    c:/cygwin/bin/ls > file.txt


    When I try to compile the following C program using GCC, it does compile, but when I run the executable, the system () command doesn't run. Anyone knows how to fix this code to make it run? Thanks.

    Code:
    #include "stdio.h" 
    #include "string.h"
    #include "stdlib.h" 
    
    main (argc, argv)
    
    
    int argc;
    char *argv [];
    
     
    {       
      system ("c:/cygwin/bin/ls > file.txt");
    
    
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I would try the unix path
    Code:
      system ("/cygdrive/c/cygwin/bin/ls > file.txt");
    Kurt

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Not only is that the ugliest function definition I've seen in a while, that's a horrible way to use system.

    First, your slashes are the wrong way around for Windows anyway.
    Second, I think that file redirections in a system command is possibly the ugliest thing you can do.
    Third, you're using the system command to list files in a directory. There are perfectly good functions for that for any given platform.
    Fourth, as mentioned, if you're in a Cygwin environment, paths may be different (and will also be affected by the correct slash which is / for Unix and \ for Windows - Hey, I didn't write the rules...)
    Fifth, almost certainly you haven't compiled with warnings enabled.

    But, really, most importantly, quite vitally? Throw away your tutorial / book that taught you to do things that way.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    3
    Quote Originally Posted by ZuK View Post
    I would try the unix path
    Code:
      system ("/cygdrive/c/cygwin/bin/ls > file.txt");
    Kurt


    I compiled this, and it doesn't work in the DOS prompt either.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    FYI:

    Some commands that run from the cmd prompt requires the prefix "cmd /c" if NOT running from the command prompt.

    It is likely that you need to do the "cd" command to verify the system command is being ran where you think it is.

    Code:
    system ("cmd /c c:/cygwin/bin/ls > file.txt");
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Mar 2013
    Posts
    3
    Quote Originally Posted by stahta01 View Post
    FYI:

    Some commands that run from the cmd prompt requires the prefix "cmd /c" if NOT running from the command prompt.

    It is likely that you need to do the "cd" command to verify the system command is being ran where you think it is.

    Code:
    system ("cmd /c c:/cygwin/bin/ls > file.txt");
    Tim S.

    Thanks Tim for your reply. I have tried that also. It works in the shell for cygwin, but running the executable in the DOS prompt,
    that system command won't run at all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. System Command: DIR
    By hqt in forum C Programming
    Replies: 8
    Last Post: 01-04-2012, 06:14 AM
  2. the system command
    By happyclown in forum C Programming
    Replies: 7
    Last Post: 01-02-2009, 07:14 AM
  3. using system command
    By warney_out in forum C Programming
    Replies: 1
    Last Post: 09-03-2004, 05:24 AM
  4. how to use system to run more than 1 dos command?
    By Jasonymk in forum C++ Programming
    Replies: 10
    Last Post: 01-03-2003, 12:01 AM
  5. system command
    By vk in forum C Programming
    Replies: 13
    Last Post: 04-03-2002, 07:44 PM