Subject Re: STUFF() returns incorrect character
From John Gillen <johngillen@cox.net>
Date Mon, 04 Jan 2021 00:01:05 -0500
Newsgroups dbase.getting-started

Akshat Kapoor Wrote:

> Good Morning John,
>
> > I am using low level file functions (FOPEN(), FEOF(), FREAD(), FWRITE() and FCLOSE() to parse a text file. As part of this parsing, I am using three commands to replace characters in the incoming file: mstring = <incoming string>; mpos = AT(<where character to replace is found in the incoming string>);  mstring = STUFF(mstring,mpos,1,"") to do the character replacement.
> >
> > If I run these three steps in the Command window, it works as expected.
> >
> > If I run these three steps in a program, instead of NULL, I get a comma (,).
> >
> > I tried "",'', and NULL CHR(0) all with the same result - a comma.
> >
> > dBASE 8/Windows 10 64bit
>
> Is by any chance , the next character.
> For eg you replace the 10th character and , is the 11th character.
>
> It would help us if you paste the exact code for the replacement.
> We do not need the full prg but just the portion between file open and
> file close.
>
> It would also help us to know if the source file is of unix origin or
> windows origin. There are minor differences.
>
> And if you can briefly outline the cause we may be able to suggest
> workarounds.
>
> Have you tried replacing the character with space
>
> Regards
> Akshat

John Gillen Wrote:

> Hello,
>
> I am using low level file functions (FOPEN(), FEOF(), FREAD(), FWRITE() and FCLOSE() to parse a text file. As part of this parsing, I am using three commands to replace characters in the incoming file: mstring = <incoming string>; mpos = AT(<where character to replace is found in the incoming string>);  mstring = STUFF(mstring,mpos,1,"") to do the character replacement.
>
> If I run these three steps in the Command window, it works as expected.
>
> If I run these three steps in a program, instead of NULL, I get a comma (,).
>
> I tried "",'', and NULL CHR(0) all with the same result - a comma.
>
> dBASE 8/Windows 10 64bit
>
> Any ideas/suggestions are appreciated.
>
> John

Hello All and thanks for the feedback,

The text file I am processing is acutally an html file from the State of California's website. I don't know the file's encoding format, but a hex view identified the  as CHR(194) in the first example below.

I could open the html file and save it as ANSI, but I was hoping to avoid that step if possible, as there are hundreds of these files. So, I opted to use the low level commands to see if I could clean up the wayward characters.

I have used STUFF() in many other programs, but this is the first time I have used it in processing an html file.

Here's the code for the test file. (TestOut.log is just for troubleshooting) I normally use an .h file, but this was a quick proof of concept test. In this version, I was testing CHR(00), but I have tried "", '' and CHR(00):

mchaptest = FOPEN("Test.html","R")
mtestout  = FCREATE("TestOut.txt","W")
mchaplog  = FCREATE("TestOut.log","W")

DO WHILE .NOT. FEOF(mchaptest)
  mstring = FGETS(mchaptest)
  moutstr = "Current string: " + mstring
  FWRITE(mchaplog,moutstr)
  FWRITE(mchaplog,CHR(10)+CHR(13))

  DO WHILE CHR(194) $ mstring
    moutstr = "Testing for CHR(194)"
    FWRITE(mchaplog,moutstr)
    FWRITE(mchaplog,CHR(10)+CHR(13))
    mpos = AT(CHR(194), mstring)
    mstring = STUFF(mstring,mpos,1,CHR(00))
  ENDDO

  DO WHILE CHR(195) $ mstring
    moutstr = "Testing for CHR(195)"
    FWRITE(mchaplog,moutstr)
    FWRITE(mchaplog,CHR(10)+CHR(13))
    mpos = AT(CHR(195), mstring)
    mstring = STUFF(mstring,mpos,1,CHR(00))
  ENDDO

  * write the results
  FWRITE(mtestout,mstring)
  FWRITE(mtestout,CHR(10)+CHR(13))
ENDDO

* close Chapters file
FCLOSE(mchaptest)
FCLOSE(mtestout)

Here are sample lines subject to STUFF() and the output:
</span> <span style="font-size: 13pt">February 13, 2020.
This produces:
</span> ‚  <span style="font-size: 13pt">February ‚  13, ‚  2020.

submitting to the voter’s county elections official
This produces:
submitting to the voter ¢â‚¬â„¢s county elections official

2119.5.</h6>Â (a)Â From the 14th day
This produces:
2119.5.</h6> ‚  (a) ‚  From the 14th day

(2) The voter’s former residence
This produces:
The voter ¢â‚¬â„¢s former residence

Thanks again.

John