Discussion:
Need Help with Text File Import
(too old to reply)
charles
2008-10-22 16:04:04 UTC
Permalink
It looks like it shows up fine here, but when I use the notepad, it's all in one line. I can't import the text file into the table, as it truncates to record number one. (the strange thing is, under windows command prompt's edit, the text shows up fine.). Obviously I'm having some problem with control characters. How do I get around this problem.
Eric Logan
2008-10-22 17:02:57 UTC
Permalink
Post by charles
It looks like it shows up fine here, but when I use the notepad, it's all
in one line. I can't import the text file into the table, as it truncates
to record number one. (the strange thing is, under windows command
prompt's edit, the text shows up fine.). Obviously I'm having some
problem with control characters. How do I get around this problem.
Maybe there is some user-built dBase utility that would allow you to read
your file directly into a table. But here's some advice based on the
assumption you will modify and read your text file with low-level functions
before appending the data into a table.
First, you apparently have some type of line terminator, but not the newline
pair : chr(13)+chr(10). So you need to find out what that line terminator
is. You can use the file object.
f = new file(); f.open(<your file>); mstr = f.read(1000)
start checking for one of the common line terminators
?at(chr(13)+chr(10), mstr)
?at(chr(13), mstr)
?at(chr(10), mstr)
Look in help class file, topic gets() for other common line terminators.
So lets say you discover that the line terminators are chr(10).
You can create another file into which you will write the output file.
g = new file()
g.create(<output file>,'RW')
Now you read the file line by line, writing the result into your new file.
puts() will add on chr(13)+chr(10) by default.
f.seek(0)
do while not f.eof()
mstr = f.gets(1000, chr(10))
g.puts(mstr)
enddo
etc.
This should give you a file you can read via modi file, etc.
If that file has a regular spacing of data values, you might be able to
append it into a table as SDF. (You would have to construct a table having
the same width as in the text file for each field.) If the values are
separated by tabs, chr(9), then you might need to read the file again to
convert the tabs to commas. Then you could append it as a delimited text
file. That's a little info on basic methods.
But I think there is a utility in the dUFLP that imports tab-separated text
files. Mabe it also reads text files with non-standard line terminators.
E.L.
Greg Hill
2008-10-22 17:11:19 UTC
Permalink
I'm trying to import a text file full of orders into a table.
When I open the text file with a notepad, the entire order lines are on
one line. At the end of the order line, where it should have broken into a
new line, a square character appears. So it's not breaking off into a new
line. Attached is the test file. How do I get around this problem?
--------------------------------------------------------------------------------
PID=XXXXXX XXXXXXX SID=9999999 XXXXXXX START 081021 2.2.0 81021.020Fz
S000009999999999999 AUVI9999999999999999
0000000027098401002 081021tst652IUNN KNRVIT
S000009999999999999 AUMC9999999999999999
0000000027098401002 081021tst654I3NN 01 KNRVIT
S000009999999999999 AUVI9999999999999999
0000000027098401002 081021tst655IUNN KNRVIT
S000009999999999999 AUVI9999999999999999
0000000027098401002 081021tst656IUNN KNRVIT
EOFEOFEOF
You will need to create your own import routine, the following is some code
to get you started.

Save the following to a program file, make the changes to honor your file
and field names.
//------start--------
use yourTable // dbf table to receive data
f = new file()
f.open("c:\yourfile.txt")

do while not f.eof()
cLine = f.gets( 10000, chr( 0x0A ) ) // Soft carriage return (U.S.)
cField1 = subs(cLine,1,6)
cField2 = subs(cLine,7,10)
// continue with your field definitions
// Now
append blank
replace yourTable->field1 with cField1
replace yourTable->field2 with cField2
enddo
return
//-------end--------
Hope this helps.

Greg Hill
Mervyn Bick
2008-10-22 17:43:08 UTC
Permalink
I'm trying to import a text file full of orders into a table.
When I open the text file with a notepad, the entire order lines are on
one line. At the end of the order line, where it should have broken into
a new line, a square character appears. So it's not breaking off into a
new line. Attached is the test file. How do I get around this problem?
What program produces the text file?
Are you in a position to change the way the text file is created?
Do you have access to a hex editor? ( XVI32 at
http://www.chmaas.handshake.de/delphi/freeware/xvi32/xvi32.htm is free and
easy to use.)


If you can look at the text file in a hex editor you will be able to see
exactly what is at the end of each line. Windows expects 0D 0A. (The hex
values for 13 and 10.)

If you are not able to influence the way the text file is created you will
have to have a little program to replace whatever is actually at the end
of the line with a carriage return and line feed or build that into the
program that appends the text file to your table. The actual program is
really simple and most of the work has been done in the example given in
the OLH under file class. It appears that there is one spurious character
in the text file so the example in the OLH will have to be amended to skip
one character before reading the next line. If there are 2 spurious
characters then you will have to skip two.

Of course, if you know what is at the end of each line you can simply read
until you reach whatever's there. Use that hex editor. <g>

Mervyn.
Greg Hill
2008-10-22 18:34:36 UTC
Permalink
It's working now. I used Greg Hill's program and it's working like a
charm.
Thanks guys. I can always feel rest easy knowing there is a place to ask
questions and get right answers.
Your welcome,
Heck we even get more than one solution most of the time..
This newsgroup has been a life saver for me at times.

Greg Hill
charles
2008-10-22 18:31:13 UTC
Permalink
It's working now. I used Greg Hill's program and it's working like a charm.

Thanks guys. I can always feel rest easy knowing there is a place to ask questions and get right answers.
Loading...