Welcome! Log In Create A New Profile

Advanced

Reading a Comma Delimited Sequential File

Posted by Glenn Rathke 
Glenn Rathke
Reading a Comma Delimited Sequential File
April 11, 2008 06:21PM
Hi,

I'm needing to read a sequential file (NP doing that). The file format is the standard format of the record delimiter being a CRLF.

Each field is enclosed in double quotes, and separated by a comma.

Is there anyway of specifying this in the reading of the file or do I have to manage everything. The problem being, a field within the double quotes may have a CRLF.

In the language I came from, you could specify if it is a DOS, BASIC, etc. file and this would automatically be handled for you.

I just need to know if there is a specific way to define the file or do I manage this myself in code.

Thanks
Glenn
Harry
Re: Reading a Comma Delimited Sequential File
April 11, 2008 07:36PM
Hi Glenn

Maybe you can use:

li_filenumber is int
ls_text is string
ls_linetext is string

li_filenumber=fopen("<YourFile>")
ls_text=freadline(li_filenumber)
while ls_tekst <> EOT
... do somethin
ls_linetext=Exractstring(ls_text,1,";")
ls_linetext=replace(ls_linetext,charact(34),"")
etc etc
...
ls_text=freadline(li_filenumber)
end

Is this what you mean ??

Harry
Glenn Rathke
Re: Reading a Comma Delimited Sequential File
April 11, 2008 08:32PM
Hi,

Thanks for the code.... but not exactly what I am looking for.

In another language whn I defined I was using a DOS or BASIC (can't remember whihc one) The file driver knew that the fields would be enclosed in double quotes and separated by a comma. It also knew that each record would be delimited by a CRLF or CR.

And since each field was within quotes, it didn't matter if there were embedded CR or commas.

Just wanting to make sure I wasns't making work for myself having to code it.

By the way, I've got it working flawlessly. I just read in the entire file, replace some characters a few times, and then use extractstring.

Thanks
Glenn
Andres Sanchez
Re: Reading a Comma Delimited Sequential File
April 11, 2008 10:47PM
Hi Glenn:

I had the same problem and send a request to PcSoft, and I think that is being solved in version 12, but for now, I pass the routine I had to make in order to archive the same thing.

And don't be ashamed to say your previous language was "Clarion". And it was the BASIC driver that did that.

Hope it helps.

PS. just put that routine into the Global Procedures and call it as the "extractString", but in this one you don't have to tell the separator, it is a comma always. Feel free to change the code and optimize it if you can, or add functionality and send it back to me.

PS#2. Sorry Fabrice....no comments at all, but it is self explanatory... (as always).

Andres Sanchez
Monterrey Mexico


PROCEDURE extractStringCSV(RecibeString is string,nIndex is int)
sResultString, sUnString are string
x1,x2,x3,x4,x5 are int

sUnString = RecibeString
LOOP (nIndex)
sResultString = ""
x1 = Position(sUnString,",")
IF x1 = 0 THEN x1=Length(sUnString)+1
x2 = Position(sUnString,"""")
IF x2=0 OR x1<x2 THEN
sResultString = sUnString[[1 TO x1-1]]
sUnString = sUnString[[x1+1 TO ]]
ELSE
x3 = Position(sUnString,""",",x2+1,FromBeginning)
IF x3=0 THEN x3 = Length(sUnString)
sResultString=sUnString[[x2+1 TO x3-1]]
sUnString=sUnString[[x3+2 TO]]
LOOP
x4 = Position(sResultString,"""""")
IF x4 = 0 THEN BREAK
sResultString = sResultString[[1 TO x4]] + sResultString[[x4+2 TO ]]
END
END
END
IF sResultString="" THEN sResultString=EOT

RESULT sResultString



Edited 1 time(s). Last edit at 04/11/2008 10:51PM by Andres Sanchez.
Glenn Rathke
Re: Reading a Comma Delimited Sequential File
April 12, 2008 03:32AM
Andres,

Thanks so much for your comments and code.... you're right about it being Clarion. It was super simple using that. But the code to accomplish the same in WebDev was not difficult.

It was great meeting you at the Nashville Conference.

Glenn
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: