Solved

Redirection of DOS output

Posted on 1997-04-14
6
324 Views
Last Modified: 2010-04-06
I am writing a Delphi 1 shell for a 16-bit dos grep.

How do I, without calling a dos batch file, use delphi code to redirect the output of a call to a dos command to a file?

An equivalent dos command line would be
'grep -flc foo bar.txt > baz.txt'

It is the '> baz.txt' portion for which I seek an answer.

Thanks.
0
Comment
Question by:laowai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 3

Expert Comment

by:mheacock
ID: 1335389
Is the DOS GREP your own creation?  I'm guessing no, else the solution would be fairly simple.
0
 

Author Comment

by:laowai
ID: 1335390
No, the 16 bit grep is not my own.  However, sample code for an *equally fast* grep would be a handsome substitute for an answer to the first question.  

I have found two problems with the following algorithm, implemented in 16 bit Delphi:
    1) Take search string
    2) Take line from file searched.
    3) If search string is in searched line, return searched line
    4) Repeat until no more lines in searched file.

Problem 1: It is not as quick as grep.

Problem 2: It bails on large files.  (I am specifically working with dictionaries, 9mbs etc.)  16 bit Grep, of course, doesn't bail.  What's it doing differently?

Related problem: The grep delphi components going around all only return whether the search file contains the string; they are not as versatile as grep.  The majority of these components are wrappers for the old regexp.dll.

aloha.
0
 
LVL 1

Expert Comment

by:flisakow
ID: 1335391

I suspect the fast versions of grep are constructing a
finite state machine to check a line for the pattern, rather
than just checking it in a naive way.

They might be reading the file in large blocks also, checking the entire block for the pattern, then retro-actively figuring out where the line was when the pattern is found.

You should be able to get a free copy of a grep code from Gnu or
a Linux site.  Also, DJGPP includes egrep and fgrep, but they
are 32-bit, even though they run on top of DOS.

Shaun
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:kolarb
ID: 1335392
If you need grep you might just have it. The source code for grep was included with Pascal7 as a Turbo Vision example.
0
 
LVL 4

Accepted Solution

by:
erajoj earned 150 total points
ID: 1335393
This is a really simple problem:
Just use command.com something like this:
  WinExec('command /c grep -flc foo bar.txt > baz.txt'...

You can also use CreateProcess if you want to wait for
the grep to finish before your shell continues.

Hope this helps.
0
 
LVL 1

Expert Comment

by:hustch
ID: 1335394
I assume you are creating a dos program.

You have 2 options :

a) Use command.com to execute your program and redirect the output. This is simple, but uses memory for additional command.com, and you can't control output to STDERR (see below).

b) Pipe the output our self :
Piping output from at dos program (using > tofile.txt) works,
because the dos program writes output to standard output (STDOUT) and/or standard error (SDTERR). When a program is invoked, these files has already been opened by command.com, so all you have to do is create a new file (or two) for the output, and then set then handle of STDOUT (and optionally STDERR) to the new handle(s).
Here is an example from a C program of mine (in Pascal notation):

const
  STDOUT = 1
  STDERR = 2

// preserve old stdout and stderr
OldOutHandle := dup(STDOUT);
OldErrHandle := dup(STDERR);

// create file for output
f = filecreate ('MyFile.TXT');

{ redirect standard files to output file
  by duplicating the file handle onto
  the file handle for standard files. }

dup2(f, STDOUT);
dup2(f, STDERR);

// close the handle for output file
fileclose(f);

 Here, you execute the program

// restore original standard handles
dup2(OldOutHandle, STDOUT);
dup2(OldErrHandle, STDERR);

// close duplicate handle for standard files
fileclose(OldOutHandle);
fileclose(OldErrHandle);

The C Dup function can be replaced by a DOS call (INT 21H)
with ah = $45, bx = handle.
At return, carry is set if error, AX = error code (4 = to many files open, 6 = invalid handle).
If OK, ax is the new handle (result of dup)

The C Dup2 function can be replaced by a DOS call (INT 21H)
with ah = $46, bx = handle of your file, cx = handle to duplicate.
At return, carry is set if error, AX = error code (4 = to many files open, 6 = invalid handle).

Let me know, if you need further help
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question