How To Insert a Line into a Text File Using Low-level File Functions (234212)



The information in this article applies to:

  • Microsoft FoxPro for Windows 2.6
  • Microsoft FoxPro for Windows 2.6a
  • Microsoft Visual FoxPro for Windows 3.0
  • Microsoft Visual FoxPro for Windows 3.0b
  • Microsoft Visual FoxPro for Windows 5.0
  • Microsoft Visual FoxPro for Windows 5.0a
  • Microsoft Visual FoxPro for Windows 6.0
  • Microsoft Visual FoxPro for Macintosh 3.0b

This article was previously published under Q234212

SUMMARY

You can add lines of a text into a text file by creating a function using the code in the MORE INFORMATION section of this article.

MORE INFORMATION

Save the following code to a program (PRG) file. The calling convention is :
Syntax:      InsertText(expC1, expN, expC2)

Parameters:  expC1  The file into which text is inserted
             expN   The line number after which text is inserted
             expC2  The text to be inserted
Returns:     Numeric
				
*-- Function InsertLine
*-- Description: Inserts text into a file at a specified line
*--
*-- Parameters:
*-- lsFileName - The file into which text is inserted
*-- liLineNum  - The line number after which text is inserted
*-- lsText     - The text to be inserted
*-- Returns:
*-- -1 = File could not be opened
*-- -2 = Text insertion failed
*-- -3 = File could not be closed

Function InsertLine
Parameter lsFileName, liLineNum, lsText
liFile = Fopen(lsFileName, 12)
If liFile = -1
    Return -1
Endif

*-- Loop until we get to the desired line
For i = 1 To liLineNum
    =Fgets(liFile)
Endfor

*-- Insert the text
liRc = Fputs(liFile, lsText)
If liRc = 0
    Return -2
Endif

*-- Clean up
llRc = Fclose(liFile)
If !llRc 
    Return -3
Else
    Return 1
Endif
				

Modification Type:MinorLast Reviewed:7/1/2004
Keywords:kbhowto kbXBase KB234212