Page 1 of 1

How to delete a given line in a text file?

Posted: Fri Aug 24, 2018 12:22 pm
by Eugene Lutsenko
How to delete a given line in a text file?
How to remove the 1st line in a txt file?

Re: How to delete a given line in a text file?

Posted: Sat Aug 25, 2018 8:10 am
by rdonnay
Here is some code that will work for you:

Usage: DELLINE <infile> <outfile> <linenumber>

DelLine.Prg:

Code: Select all

#INCLUDE "dcdialog.CH"

FUNCTION Main( cInFile, cOutFile, cLineToDelete )

LOCAL nHandle1, nHandle2, aText[0], cLine, nLineToDelete, nLine

nLineToDelete := Val(cLineToDelete)
nHandle1 := DC_TxtOpen(cInFile)
nHandle2 := FCreate(cOutFile)

nLine := 1
DO WHILE !DC_TxtEof(nHandle1)
  cLine := DC_TxtLine(nHandle1)
  IF nLineToDelete <> nLine
    FWrite(nHandle2,cLine+Chr(13)+Chr(10))
    ? cLine
  ELSE
    ? '<<<<<<<< Line ' + cLineToDelete + ' has been deleted >>>>>>>>>'
  ENDIF
  DC_TxtSkip(nHandle1)
  nLine++
ENDDO

FClose(nHandle1)
FClose(nHandle2)
wait

RETURN nil
DelLine.xpj:

Code: Select all

[PROJECT]
    COMPILE       = xpp
    COMPILE_FLAGS = /q /w /rDCLIPX.LIB
    GUI           = yes
    LINKER        = alink
    DelLine.XPJ

[DelLine.XPJ]
    DelLine.EXE

[DelLine.EXE]
    DelLine.PRG

Re: How to delete a given line in a text file?

Posted: Sat Aug 25, 2018 11:23 am
by Eugene Lutsenko
Thank You, Roger! I have the following code. It works correctly, except at the beginning of the file is an empty string, which is unnecessary.

Code: Select all

          mFileName = SUBSTR(aFileName[ff], 1, AT('.',aFileName[ff])-1)
      
          CLOSE ALL
          USE (mFileName) EXCLUSIVE NEW
          SELECT (mFileName)
      
          ********** Открыть процесс печати выходной формы
          set device to printer
          set printer on
          set printer to (mFileName+'.csv')
          set console off
      
          ?'SK_ID_CURR,TARGET'
      
          DBGOTOP()
          DO WHILE .NOT. EOF()
          
             ?ALLTRIM(ID)+','+ALLTRIM(STR(ROUND(PROB,1)))
      
             DBSKIP(1)
          
          ENDDO
      
          ********** Закрыть процесс печати выходной формы
          Set device to screen
          Set printer off
          Set printer to
          Set console on
[/size]

An example of the resulting file:

Code: Select all


SK_ID_CURR,TARGET
100001,0.9
100005,0.8
100013,0.7
100028,0.3
100038,0.8
100042,0.5
100057,0.8
100065,0.7
100066,0.7
100067,0.8
100074,0.8
100090,0.8
100091,0.8

Re: How to delete a given line in a text file?

Posted: Sat Aug 25, 2018 11:28 am
by Wolfgang Ciriack
Change ?'SK_ID_CURR,TARGET' to ??'SK_ID_CURR,TARGET'

Re: How to delete a given line in a text file?

Posted: Sat Aug 25, 2018 11:40 am
by Eugene Lutsenko
Thank you Roger and Wolfgang for the great tips!

Everything worked out great.

And I apologize for the fact that I can not always clearly and concretely formulate the question. But a good question is already part of the answer. And when you do not know the answer, and the question is clearly difficult to formulate.