| Subject |
Re: CSV line termination issue |
| From |
Ken Mayer <dbase@nospam.goldenstag.net> |
| Date |
Thu, 8 Oct 2020 20:51:15 -0700 |
| Newsgroups |
dbase.getting-started |
On 10/8/2020 3:21 PM, Gaetano wrote:
>
> Hi All,
>
> I have an input CSV file that has a CHR(10) (\n - hard line feed) at the
> end of each line. When I f.gets(1000), dbase returns the first 1000
> characters and ignores that end of line character.
>
> When I f.gets(1000,chr(10)), it works, but then it may not handle
> correctly a CSV file created in Windows.
>
> What is a good way to handle an unknown end of line character? How do I
> test for the end of line character since gets() stops at the first end
> of line character it finds? I tried the following
>
> cRead = f.gets(1000)
> ASC(substr(cRead,167,1)
>
> and that returned 0 (zero) on a Windows file but I'm not sure whether
> that is a hidden end of line character in the string (a European soft
> line feed as per gets() help) or whether ASC() returns zero because
> substr() returns a NULL since start position 167 is past the end of string.
>
> I do know that the length of the first line MUST be 166 characters (it's
> a control I hard-code to make sure the app gets the right loader file
> structure).
>
If the lines always end in chr(10), then you need to alter them to
chr(10)+chr(13).
The program below works for me for dealing with the EOL issue ... it
also makes a backup of the file in case something fails.
Ken
/*
FROMUNIX.PRG
A simple program using two file objects to read
a file with "LF" (line feed) as the end of line
character -- DOS/Windows prefer "CR/LF" (Carriage
Return and Line Feed).
Updated February, 2015 to work with dBASE Plus 9.5.
** Assumptions:
fMessage is open from calling program -- this is
an instance of message.wfm in the dUFLP. Only
occurs in runtime (which allows testing outside
of the runtime).
Makes a backup copy of the input file (filename2)
Copyright ©1999-2016
Golden Stag Productions
Ken Mayer
*/
parameter cInFile
if pCount() < 1
msgbox( "Need a filename to process!", "Can't do it!", 16 )
return
endif
if not file( cInFile )
msgbox( "File does not exist!", "Can't do it!", 16 )
return
endif
cBackupFile = cInFile+"2" // add a '2' to end of filename
set safety off
copy file (cInFile) to (cBackupFile)
set safety on
fReadIt = new File()
fReadIt.Open( cBackupFile, "R" )
// Check to see how long the first line
// of the file is:
cString = fReadIt.gets( 1000 ) // 1,000 characters
nLength = cString.length
fReadIt.seek( 0 ) // back to the top of the file
// if the first line is wider than 80 characters,
// we need to convert this file ...
nLines = 0
if nLength > 80
fWriteIt = new File()
fWriteIt.Create( cInFile, "A" )
do while not fReadIt.eof() // loop
// 10000 = an arbitrarily large value for the
// number of characters to read
// chr( 0x0A ) = the Line Feed character -- information
// for these is in online help
cString = fReadIt.gets( 10000, chr( 0x0A ) )
// read a line to the Line Feed
// this places the position pointer
// on the next line ...
// the writeLn method writes out the CR/LF automatically:
fWriteIt.writeLn( cString )
nLines++
if "runtime" $ lower(version(1))
if int( nLines/50 ) == nLines/50
fMessage.message.text := nLines+" lines converted to ASCII ..."
_app.executeMessages()
endif
endif
//? cString
enddo
fWriteIt.close()
if not "runtime" $ lower(version(1))
? nLines+" lines converted to ASCII ..."
endif
endif
fReadIt.close()
/*
End of file: FROMUNIX.PRG
*/
--
*Ken Mayer*
Ken's dBASE Page: http://www.goldenstag.net/dbase
The dUFLP: http://www.goldenstag.net/dbase/index.htm#duflp
dBASE Books: http://www.goldenstag.net/dbase/Books/dBASEBooks.htm
dBASE Tutorial: http://www.goldenstag.net/dbase/Tutorial/00_Preface.htm
|
|