[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Redirection of DOS output

Posted on 1997-04-14
6
Medium Priority
?
327 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
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
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!

 
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 450 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month19 days, 21 hours left to enroll

873 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