TurboC again

Steve Harrold swh at hpcupt1.HP.COM
Thu Feb 1 02:52:37 AEST 1990


Re: TurboC problem

>>> The statement:
>>> 
>>>   system("sort <c:\\directory\\filename.ext >c:\\directory\\filename.ext");
>>> 
>>> causes some odd behaviour. The applications spawns to the DOS level, but 
>>> does not execute the command...
----------

The problem lies in the fact that the "system()" call executes the named
program directly without benefit of the "command.com" shell.  Because of this,
the file redirection intent is not handled by a shell; the "sort" program
sees the "<.." and ">.." strings as parameters, which it does not know
what to do with.  If a shell were present, it (the shell) would process the
"<.." and ">.." strings, remove them from the command line, and assign the
named files to "stdin" and "stdout".  The "sort" program would perceive no
parameters.

To achieve the intended file redirection you would have to either

1) use something like 

	system("command /c sort <... >...");

2) or, use the dup() and dup2() library routines to re-assign where "stdin"
   and "stdout" are to come from before using the simpler

	system("sort") ;

Hope this helps.



More information about the Comp.lang.c mailing list