Subject Re: STUFF() returns incorrect character
From Mervyn Bick <invalid@invalid.invalid>
Date Sun, 3 Jan 2021 11:01:57 +0200
Newsgroups dbase.getting-started

On 2021/01/02 19:53, 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

From dBASE 9 onwards dBASE uses different base classes internally and a
different compiler is used to create the program.  I no longer have
dBASE 8 available so I can't test it but dBASE 2019 has no problem with
using stuff() to replace a character in a string.  dBASE 8 should also
not have a problem but without seeing the actual code it is not possible
to make any suggestions.

Where stuff() is used to replace 1 character with "" it actually removes
the specified character from the string.

The low level file functions still work in dBASE to take care of legacy
programs but, as Gaetano has suggested, using the file class is
preferable.  Apart from anything else, the syntax is actually a little
simpler as you no longer need to get file handles yourself.

cInfile = 'whatever_in.txt'
cOutFile = 'whatever_out.txt'
cRemove = '\'
fIn = new file()
fIn.open(cInFile) //ready to be read
fOut = new file()
fOut.create(cOutfile) // will overwrite an existing file
do while not fIn.eof() //read line by line to end of file
    cRead = fIn.readln(10000) //any value > than longest line
     if cRemove$cRead  // do we need to deal with this line?
        do while cRemove$cRead // loop in case there are several characters
           cRead = stuff(cRead,at(cRemove,cRead),1,"")
        enddo
     endif
     fOut.writeln(cRead)
enddo
fIn.close()
fOut.close()

This example only deals with removing a given character completely from
a file.  If you give more details of exactly what the data in the input
file looks like and how you want to parse it there may be a different
solution.

Mervyn.