Need to open and read text file.

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

NickSteel
Posts: 67
Joined: Mon May 27, 2013 10:44 pm

Need to open and read text file.

Post by NickSteel »

Could I trouble you one more time? I wanted to save cLine to a file while it processed.

Code: Select all

If nArquivo # -1
   Do While !lEof   .and. ctr<10
      cLine := CharRem( Chr(26), ReadLine(nArquivo,LINEBUFF) )
     Aadd(aLine,cLine)
     msginfo(cline)
     fwrite(outfile,cLine)
     ctr++
   Enddo
Endif
This doesn't seem to work. I am having trouble with fwrite. Don't know what I'm doing wrong.
[u]Moderator Notes[/u] (Pablo César) wrote:Message re-edited to put the tag 

Code: Select all

 [/b]

Please note tag CODE is recommended when it is source code samples posted for better reading.[/color][/quote]
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

Re: Need to open and read text file.

Post by Pablo César »

NickSteel wrote:Could I trouble you one more time? I wanted to save cLine to a file while it processed.
No problem.

Code: Select all

nFhandle2:= Fcreate( 'File.txt', 0 )
If nFhandle2 > 2
   Do While !lEof   .and. ctr<10
      cLine := CharRem( Chr(26), ReadLine(nArquivo,LINEBUFF) )
     Aadd(aLine,cLine)
     FWriteLine( nFhandle2, cline )
     ctr++
   Enddo
Endif

Static Function FWriteLine( nHandle, cString )
Fwrite( nHandle, cString )
Fwrite( nHandle, chr(13) )
Fwrite( nHandle, chr(10) )
Return Nil
You can use FWriteLine for many files, depending always nFhandle from Fcreate you do.
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
NickSteel
Posts: 67
Joined: Mon May 27, 2013 10:44 pm

Re: Need to open and read text file.

Post by NickSteel »

Wow. I just processed a 2400 line file in a 6 seconds on an old dual core laptop! Had to make LINEBUFF very large, but it worked like a champ.

You've made my day. :)
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

Re: Need to open and read text file.

Post by Pablo César »

Remember: these functions are in LOW LEVEL. The best way to access files and read strings, also binaries. Study all these low level functions, very very useful.

I am happy to be helpfull... :D
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
NickSteel
Posts: 67
Joined: Mon May 27, 2013 10:44 pm

Re: Need to open and read text file.

Post by NickSteel »

Since you're a whiz with low level functions, do you have suggestions for parsing the tab delimited data while it is in the buffer? I've been searching sites and have read that it is much faster than writing to a string variable and parsing that. What I wish to do is read lines from a Primavera P6 XER export file (text delimited with tabs), select some "fields" of data and update existing DBF files. Your code is lightning fast at accessing the input text, but I haven't been able to find any existing parsing functions.

Your aid is very appreciated.
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

Need to open and read text file.

Post by Pablo César »

I learned much including low level functions, with this guy. There is also good and friendly atmosphere in Clipper on Line forum.

Could you post some sample file in you next post ? Just to see it in hexadecimal editor and check which exactly is needing for.

For parsing characters even in the buffer it could be done. You will find an example at STRU.prg ( here it is the package project, without executable file) and look up for "Function Le_Variavel(cBuff)"
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
User avatar
srvet_claudio
Posts: 2220
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Contact:

Re: Need to open and read text file.

Post by srvet_claudio »

Pablo César wrote:
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...
Since version HMG.3.1.3

Code: Select all

MsgDebug ( xData1, xData2, ... )  --> Accepts as a parameters any data type, elements or objects.
Returns a string with the message displayed on the screen.
 
Example:
Num   := 10
aData := { "Number", 38, "aRGB", YELLOW, "Hello" }
cMsg  := MsgDebug ( TIME(), aData, {|| NIL}, Num == 3 )
Best regards.
Dr. Claudio Soto
(from Uruguay)
http://srvet.blogspot.com
NickSteel
Posts: 67
Joined: Mon May 27, 2013 10:44 pm

Re: Need to open and read text file.

Post by NickSteel »

Can't attach txt or xer files.
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

Re: Need to open and read text file.

Post by Pablo César »

srvet_claudio wrote:Since version HMG.3.1.3

Code: Select all

MsgDebug ( xData1, xData2, ... )  --> Accepts as a parameters any data type, elements or objects.
Returns a string with the message displayed on the screen.
 
Example:
Num   := 10
aData := { "Number", 38, "aRGB", YELLOW, "Hello" }
cMsg  := MsgDebug ( TIME(), aData, {|| NIL}, Num == 3 )
Dr. Soto Thanks for reminding us! Had not stopped to see such a function.
Nick wrote:Can't attach txt or xer files.
Try to attach using WinRar. Probably it due for forum security reasons.
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
NickSteel
Posts: 67
Joined: Mon May 27, 2013 10:44 pm

Re: Need to open and read text file.

Post by NickSteel »

My source file.
Attachments
K3.zip
Many lines, many formats.
(443.92 KiB) Downloaded 276 times
Post Reply