Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

appending a text file

Status
Not open for further replies.

Center

Programmer
Oct 19, 2002
52
US
Have a text file with many lines, several words to a line,
separted by word spaces. Wish to append each word as a
separate record in a single field of a dbaseIV dbf. Can't
write the "appe from" line so a space can be used as delimiter.
Any suggestion appreciated.
 
The APPEND FROM <FILE> DELIMITED WITH BLANK must be the command you were trying to use. The problem is that dBase and most other programs, I presume, read in one line per record but you are trying to read in one word per record which may not be possible. The above command will place one word in each line into separate fields.

SOURCE TEXT FILE:
Hello friends,
The sun shines brightly.

APPENDED INTO TABLE:
Record 1, Field 1: Hello
Record 1, Field 2: friends,
Record 2, Field 1: The
Record 2, Field 2: sun
Record 2, Field 3: shines
Record 2, Field 4: brightly.

What you're trying to do will require some coding, but as with most programming there's always more than one way to do it. Here is what I think should be the most simple:

You could append the lines into a work table but in that case you would (1) need to be sure you had enough fields for all the words on the incoming line and (2) be sure every field was large enough for the largest word you could encounter. Then after appending the text file you could skip through the table, record by record, field by field, to copy each word one by one to the final table holding one word per record. That would also be the time to adjust upper/lower case, remove punctuation marks, check for word duplications, etc.

 
I've done this in the past as well, this is how I went about it;
//declare variables
Private cline, d, q, qt, i, xWord
* Open file and Determ how many spaces there are in the *text.

#Define TextFile &quot;C\Temp\Textfile.TXT&quot;
f = new File()
if f.exists(TEXTFILE) //Does the file exist?
f.open(TEXTFILE, &quot;A&quot;) //Open in archive mode.
else
* do something if does not exist.
endif

Create Table &quot;TextTable.dbf&quot; (;
WordNum Number(5),;
Word Char(32)) // Enlarge if necessary
f.seek(0) //position the cursor at the begining of file.

d = new Database() // initiate Database (OODML)
d.databaseName = &quot;Text&quot; //Describe Database
d.active = True // make active

q = new query() //intiate query
q.database = d // assign database
q.Sql = 'Select * from &quot;TextTable&quot;' //assignment of table
q.active = True // make active

qt = q.rowset
i = 1
do
cLine = f.gets( 10000, chr( 0x8d ) ) // Soft carriage return (U.S.)
x = 0
y = x
do
y = cline.indexof(Chr(32), x) //location of space
xWord = cline.substring(x, y).lefttrim().rightTrim()

qt.beginappend()
qt.fields[&quot;Wordnum&quot;].value = i
qt.fields[&quot;word&quot;].value = xWord
qt.save()

i++ //increment 1 for next WordNum field value
y++ //move the starting point over 1
x = y //x == y as the new starting point of next word
until y = -1 //indexof seach fails.
until f.eof //continue till end of textfile

f.close() //close textfile
q.active = False // close tables
d.active = false // close database

release cline, d, q, qt, i, xWord
return

//you may have to make some adjustments depending on your textfile and the carriage returns used in its construction.
you can also include a variable for the line number and create an additional field in the create table statement to capture that information.

For XDML language you will need to do a Use statement to open tables and an Append Blank with Replace statement to insert data into table. if you need more info email me.

have fun.
RRiley
 
Thank you both for the good suggestions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top