Page 1 of 5

Need to open and read text file.

Posted: Tue May 28, 2013 12:22 pm
by NickSteel
I need to parse a tab delimited text file into a DBF.

Each line ends with ascii(182). Need to read line by line so I can parse and format the data and then put into the DBF. Example code would be most appreciated.
[u]Moderator Notes[/u] (Pablo César) wrote:Message and title have been re-edited also replaced from Self Introduction section, because it contains technical questions. And message/topic title was re-edited.

Hi Guys. Need to open and read text file.

Posted: Tue May 28, 2013 12:30 pm
by Pablo César
Have you well come to forum !

You need to make your own routine, reading your text in file, line by line. I indicate to you to do it thru low level functions (FOpen, FRead, FClose). These functions you should be aware in Clipper, it's better way and safe.

Re: Hi Guys. Need to open and read text file.

Posted: Tue May 28, 2013 12:33 pm
by esgici
NickSteel wrote:I'm new to HMG (old to Clipper) and need a little information.

I need to parse a tab delimited text file into a DBF.

Each line ends with ascii(182). Need to read line by line so I can parse and format the data and then put into the DBF. Example code would be most appreciated.
Hi Nick, welcome aboard

The method may depends size of file.

If could you send a little portion ( 10-20 line ) and notify size of file, will be possible suggesting more specific way(s).

Happy HMG'ing :D

Re: Hi Guys. Need to open and read text file.

Posted: Tue May 28, 2013 1:30 pm
by NickSteel
The file is huge and has different line formats. Have to do it line by line. If I can get a line in, I can figure out how to parse it. Can you post an example with Fopen(), Fclose(). Long time since I used low level Clipper commands.

Thanks in advance.

ERMHDR 6.2.1 2013-05-09 Project xxlni8 GEP - Leo Nicholson dbxDatabaseNoName Project Management USD
%T CURRTYPE
%F curr_id decimal_digit_cnt curr_symbol decimal_symbol digit_group_symbol pos_curr_fmt_type neg_curr_fmt_type curr_type curr_short_name group_digit_cnt base_exch_rate
%R 1 2 $ . , #1.1 (#1.1) Dollar USD 3 1
%R 10 2 $ . , #1.1 (#1.1) Argentine Peso ARS 3 3.077
%R 11 2 A$ . , #1.1 (#1.1) Australian Dollar AUST 3 1.208
%R 13 2 R$ . , #1.1 (#1.1) Brazilian Real BRL 3 2.014

%T MEMOTYPE
%F memo_type_id seq_num eps_flag proj_flag wbs_flag task_flag memo_type
%R 921 1 Y Y Y Y General Comments
%R 31323 527 N N N Y SAP Order Header Long Text
%T OBS
%F obs_id parent_obs_id guid seq_num obs_name obs_descr
%R 565 0 America's Capital Projects

Hi Guys. Need to open and read text file.

Posted: Tue May 28, 2013 1:47 pm
by Pablo César
This routine I used in STRU app.

Code: Select all

#include "fileio.ch"

#define CRLF            Chr(13)+Chr(10)
#define LINEBUFF        1024


Function MakeArry(cFile)
Local i, cLine := "", aLine := {}

Private lEof:= .f.
nArquivo:=FOpen(cFile,0)
If nArquivo # -1
   Do While !lEof
      cLine := CharRem( Chr(26), ReadLine(nArquivo,LINEBUFF) )
	  Aadd(aLine,&(cLine))
   Enddo
Endif
FClose(nArquivo)
Return aLine

Static Function ReadLine(nHandle,nBuffSize)
Local cRet:= cBuff:='', nPos:= nEol:= nRead:=0

cBuff := Space(nBuffSize)
nPos := FSeek(nHandle,0,1)
If ( nRead:=FRead(nHandle,@cBuff,nBuffSize) ) > 0
   If ( nEol:=At(CRLF,SubStr(cBuff,1,nRead)) ) == 0
      cRet:=SubStr(cBuff,1)+Chr(26)
      lEof:=.T.
   Else
      cRet:=SubStr(cBuff,1,nEol-1)
      FSeek(nHandle,nPos+nEol+1,0)
   Endif
Else
   lEof:=.T.
Endif
Return cRet
I hope to be useful.

Re: Need to open and read text file.

Posted: Tue May 28, 2013 5:40 pm
by NickSteel
Thanks, guys. Especially for the sample code!

Re: Need to open and read text file.

Posted: Wed May 29, 2013 2:25 pm
by NickSteel
Trying everything and failing. Cannot get the code above to compile (syntax error & MAKEARRAY17).

I've been trying to find a sample with FOPEN(), FREAD(), FREADSTR(), FWRITE(), etc that works.

Re: Need to open and read text file.

Posted: Wed May 29, 2013 2:57 pm
by Pablo César
NickSteel wrote:Trying everything and failing. Cannot get the code above to compile (syntax error & MAKEARRAY17).
Try to replace this:

Aadd(aLine,&(cLine))

changing to:

Aadd(aLine,cLine)

And let me know is working. This macro character I've have been used due my text in file are with quotes characters at begining and final of each line. But seems is not same case for you...

Re: Need to open and read text file.

Posted: Wed May 29, 2013 3:26 pm
by NickSteel
This seems to be working. I inserted a msginfo(cLine) so I can see the text. What is easy way to display the array?

And..................

This is very clever code! :D I know it will solve my problem, as it reads the source file line by line.

Re: Need to open and read text file.

Posted: Wed May 29, 2013 3:41 pm
by Pablo César
NickSteel wrote:What is easy way to display the array?
I usually use DebugMSG function from our colleague Marek. Hereunder is:

Code: Select all

Function DebugMSG
Local i, aTemp := {}
   
For i := 1 to pcount()
    aadd( aTemp, hb_PValue(i))
Next i
MsgBox(hb_valtoexp(aTemp), "Helpful informations")
Return Nil
Put in you code and call this function like this: DebugMSG(aLine)
This is very clever code! I know it will solve my problem, as it reads the source file line by line.
Thank you and glad to know it has been helpful to you...