Home > Archive > Clipper > February 2008 > Text to array
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| Hi,
How can I read the text file into an array.
Please advise.
Derek
| |
| E. Fridman 2008-02-25, 6:55 pm |
| Derek,
What kind of result do you invision?
For example, what should become of the following text file?
Paragraph 1. Sentence One. Sentence Two.[CR][LF]
[CR][LF]
Paragraph 2. Sentence One.[CR][LF]
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
On Feb 25, 10:43=A0am, "Derek" <dalik...@netvigator.com> wrote:
> Hi,
> How can I read the text file into an array.
> Please advise.
>
> Derek
| |
|
| On Mon, 25 Feb 2008 23:43:09 +0800, "Derek" <dalikyim@netvigator.com> wrote:
>Hi,
>How can I read the text file into an array.
>Please advise.
>
>Derek
>
Sample for a fixed length text (80)
Local NbLine, Txt
Local aTexte := {} // Array
tprg := fopen("Texte.txt",64)
if tprg = -1
PrgError("Error opening TEXT file !")
end
NbLine := fs (tprg,0,2)/80 // How many line ?
fs (tprg,0)
for t := 1 to NbLine
Txt := Trim(FreadStr(tprg, 80))
aadd(ATexte,{ Txt, t })
next
Not tested!
Regards
Otto
| |
| sebas22 2008-02-25, 6:55 pm |
| On Mon, 25 Feb 2008 23:43:09 +0800, "Derek" <dalikyim@netvigator.com>
wrote:
>Hi,
>How can I read the text file into an array.
>Please advise.
*------------------------------- StrToArr --------------------------*
* Transform a string into an array, dependiing on the
* separator cSep
* Ex : StrToArr("12/34", "/") -> {"12", "34"}
* StrToArr("12/34", "$") -> {"12/34"}
* StrToArr("", "$") -> {}
* StrToArr(c, "") -> {c}
* If lKeepNul is .t., we keep empty lines ( == "").if .f., we forget
them (default : .f.)
function StrToArr(cStr, cSep, lKeepNul)
local aArr := {}, cLeft
local nPos
if ! ischar(cSep) .or. cSep == ""
return {cStr}
endif
if ! empty( lKeepNul ) // Si on veut garder les lignes vides
("")
do while cSep + cSep $ cStr
cStr := strtran( cStr, cSep + cSep, cSep + " " + cSep )
enddo
endif
nPos := at(cSep, cStr)
do while nPos > 0
cLeft := substr(cStr, 1, nPos - 1)
if len(cLeft) > 0
aadd( aArr, trim(cLeft) )
endif
cStr := substr(cStr, nPos + 1)
nPos := at(cSep, cStr)
enddo
if .not. empty(cStr)
aadd(aArr, cStr)
endif
return aArr
*---- EoP StrToArr ----*
--
Regards,
Sebas
| |
| Derek Yim 2008-02-25, 9:55 pm |
| Hi,
the text file contain following data:-
AAAA
BBB
CCCC
DDD
EEEE
FFF
How to check now many lines in the files.
How to move the pointer to next line ?
each data width is 4 Char. long.
"Derek" <dalikyim@netvigator.com> 撰寫於郵件新聞:47c2e20d$1@127.0.0.1...
> Hi,
> How can I read the text file into an array.
> Please advise.
>
> Derek
>
| |
| Stephen Quinn 2008-02-25, 9:55 pm |
| Derek
> How to check now many lines in the files.
> each data width is 4 Char. long.
Get the filesize and divide by 6
(your 4 character data with CRLF (CHR(13)+CHR(10), Carriage return/Linefeed) at the end of each line.
Depending on how the file was created there maybe a Ctl+Z (CHR(26)) (EOF indicator) at the end of the file
> How to move the pointer to next line ?
Something like below should give you an idea (untested code - I just typed it into this message).
aData := {}
cBuffer := space(256)
nHandle := FOpen( 'text.txt', FO_READONLY )
FReadLine( nHandle @cBuffer, 255 )
cData := trim( cBuffer )
do while ! Empty( cBuffer )
aadd( aData, cData )
FReadLine( nHandle @cBuffer, 255 )
cData := trim( cBuffer )
if cData = CHR(26) .OR. Empty(cData)
exit
endif
endif
FClose( nHandle )
I'll leave the error checking to you<g>.
Search on the OASIS for FBrowse, it'll show you how to browse a text file (open/read/move thru), there are plenty of
code samples on the OASIS that will also help you (the source for CLICK! for example).
NANfor.lib, GrumpFish.Lib, SuperLib, etc...
--
CYA
Steve
| |
| Ray Marron 2008-02-26, 6:55 pm |
| On Feb 25, 8:43 am, "Derek" <dalik...@netvigator.com> wrote:
> Hi,
> How can I read the text file into an array.
> Please advise.
>
> Derek
I have a function for this in my library. Download raylib.zip from
http://www.raymarron.com/clipper.
You're looking for the function LoadTextFile from the ReadLine.prg
file. Of course, it only works on text files with 4K or fewer lines.
--
Ray Marron
| |
| tom knauf 2008-02-26, 6:55 pm |
| Hi,
what about simply using MEMOREAD() and MLCOUNT() or is the file too big ?
HTH
Tom
"Derek Yim" <dalikyim@netvigator.com> schrieb im Newsbeitrag
news:47c37863$1@127.0.0.1...
> Hi,
> the text file contain following data:-
>
> AAAA
> BBB
> CCCC
> DDD
> EEEE
> FFF
>
> How to check now many lines in the files.
> How to move the pointer to next line ?
> each data width is 4 Char. long.
>
>
> "Derek" <dalikyim@netvigator.com> 撰寫於郵件新聞:47c2e20d$1@127.0.0.1...
>
>
| |
|
| Dear Derek:
On Feb 25, 8:43=A0am, "Derek" <dalik...@netvigator.com> wrote:
> Hi,
> How can I read the text file into an array.
> Please advise.
You have the obvious answers from others.
Another might be to simply
APPEND FROM <cfilename> SDF, and use dbf statistics to create and
populate the array. Or just index the file, and use it as is (the
index resides in RAM, if the file is small enough).
David A. Smith
|
|
|
|
|