Page 1 of 1

Leer archivo txt delimitado por tabulares

Posted: Sat Mar 18, 2023 11:40 pm
by jorge.posadas
Grupo

Tengo la necesidad de leer un archivo txt que está delimitado por tabuladores, nunca he leído un archivo txt con hmg, ¿alguien tiene alguna idea o rutina o si me dan un poco de luz?

De antemano gracias por la ayuda

Re: Leer archivo txt delimitado por tabulares

Posted: Sun Mar 19, 2023 4:14 am
by Red2
Hi Jorge,

I no longer have access to such code written some years ago in VFP.

Actually, you can read a text file with MEMOREAD() if the file is less than 64K
-OR- if it is greater than 64K use FOPEN(), FREAD(), FCLOSE() into a text string.

It is then pretty basic to check each character in a FOR...NEXT or DO WHILE loop.

When you find a CHR(9) it is a tab (column/field) delimiter.
A CHR(13)+CHR(10) (or such) indicates end of line (record delimiter).

I hope that this helps. I am away from home now but by mid-week should be back.
If you have any further questions I'd be able to help more them.

Best Regards, Red2

Re: Leer archivo txt delimitado por tabulares

Posted: Sun Mar 19, 2023 4:14 am
by Red2
Hi Jorge,

I no longer have access to such code written some years ago in VFP.

Actually, you can read a text file with MEMOREAD() if the file is less than 64K
-OR- if it is greater than 64K use FOPEN(), FREAD(), FCLOSE() into a text string.

It is then pretty basic to check each character in a FOR...NEXT or DO WHILE loop.

When you find a CHR(9) it is a tab (column/field) delimiter.
A CHR(13)+CHR(10) (or such) indicates end of line (record delimiter).

I hope that this helps. I am away from home now but by mid-week should be back.
If you have any further questions I'd be able to help more them.

Best Regards, Red2

Re: Leer archivo txt delimitado por tabulares

Posted: Thu Mar 23, 2023 8:56 pm
by franco
Hi jorge,
it is very simple.
You can create a table with one field to start with. Make it character with length a bit longer then the text file character length.
then you:
use table
append from textfile.txt delimited
browse ?
Your table should have the contents of the text file.
You can also create the table with a field for each item in text file (item meaning item before each in line) Like name, phone, and so on.
Then:
append from text.txt delimited with ','
If my description is confusing , if I can find a text file I could create a example for you.
Give me a short example of you text file and what you want to do with it.
Franco

Re: Leer archivo txt delimitado por tabulares

Posted: Fri Mar 24, 2023 4:42 am
by AUGE_OHR
hi,

before you can "import" Data from *.CSV you must create DBF with "matching" FIELD(s)

open *.CSV using Editor (Notepad.EXE) and have a look how data "Type" look like
when 1st Line is a "Header" you must "skip" 1st Line

---

you can to use MEMOREAD(), MLCOUNT() and MEMOLINE() to "extract" a Line of *.CSV

Code: Select all

LOCAL cMemo := MEMOREAD("C:\TEMP\MYFILE.CSV")
LOCAL ii,nMax, aToken, cDelimiter := CHR(9)
LOCAL lUseHeader := .F.

   nMax  := MlCount( cMemo, 80 ) 
   FOR ii := 1 TO nMax
      cLine := MemoLine( cMemo, 80, ii ) 
      aToken := hb_ATokens(cLine, cDelimiter)
      
      IF ii = 1 .AND. lUseHeader 
         DO_CreateDBF(aToken)
      ELSE
         DO_Replace(aToken)
      ENDIF
   NEXT
when have hole line i use hb_ATokens() to get a Array of each Value
https://harbour.github.io/doc/harbour.html

Re: Leer archivo txt delimitado por tabulares

Posted: Sat Mar 25, 2023 5:54 pm
by franco
This is the simplest way to that I know of to start
Make sure your table field length is long enough to get length of text file line.

Code: Select all

 
#include "hmg.ch"

FUNCTION Main()
local fil := 'sam.txt'
AADD( aSTRUCT_L , { 'z1 '    , 'c' , 80, 0 } )
DBCREATE( "your.dbf" , aSTRUCT_L,)
use
use  your via "dbfntx"
zap
append from &fil sdf  
use
return
Here your table will have all records one field with line from your text file.

There are other ways to append also where you create fields in the table and the file may have delimiters like commas in a csv file.
Here you would append from &fil file delimited with ","
Your dbf will have each field filled with data from text between commas.
Hope this helps.
Franco

Re: Leer archivo txt delimitado por tabulares

Posted: Sun Mar 26, 2023 4:49 pm
by franco
Here is how append from a text file works.
If you have a text csv or prn file that looks like the following.

Your textfile.txt
John, Doe,
Jack, Smith,
Tom, Tam,
Text file end.

You create a table.
YourTable.
field1 character 60 | field2 character 20 |
Table end.

If you INCODE use YourTable then append from "YourTextfile.txt" SDF
You get in your table only field1 has data field2 is empty
field1 ______________________ field2
John, Doe,
Jack, Smith,
Tom, Tam,

If you INCODE use YourTable then append from "YourTextfile.txt" DELIMITED with ","
You get in your table field1 and field 2
field1 _____________________field2
John______________________ Doe
Jack______________________ Smith
Tom______________________ Tam

End example.
I hope this is helping
Franco

Re: Leer archivo txt delimitado por tabulares

Posted: Mon Mar 27, 2023 10:45 am
by franco
Sorry I revised previous post to read more clearly.