Click to See Complete Forum and Search --> : Inserting a line in a text file


gknierim
February 14th, 2000, 12:39 PM
I was wondering what the best way would be to do the following:

I have a text file. The name of the text file is abc123.dat. What I would like to do, is open that file, and insert the name of the file (abc123 .. without the extension) as the first line of the file and then save the file. I know that I need to open the file, insert the data, and then save it but some code examples would be helpful. What should I use to insert the data into the file?

Thanks in advance for any suggestions.
Greg

Cakkie
February 14th, 2000, 12:59 PM
Use this

dim line as integer

private sub InsertLine(byval strFile as string, byval intLineNum as integer, byval strLine as string)
' strFile is the path to the file you want to use
' strLine is the text you want to insert
' intLineNum is the number of the line where you want to insert the text

dim ffileIn as integer
dim ffileOut as integer
dim strInput as string
dim intLineCount as integer

ffileIn = freefile
open strfile for input as #ffileIn

ffileOut = freefile
open app.path & "\tmp.dat" for output as #ffileOut

do while not(eof(ffileIn))

lineCount = lineCount + 1
line input #ffileIn, strInput

if lineCount = intLinenum then
print #ffileout, strLine
end if

print #ffileout, strInput

loop

close #ffilein
close #ffileout

filecopy app.path & "\tmp.dat", strFile

end sub



This should do the trick

Tom Cannaerts
slisse@planetinternet.be

The best way to escape a problem, is to solve it.

gknierim
February 14th, 2000, 01:32 PM
Absolutely the perfect code...except...instead of being a Private Sub, you might want to try Private Function!! :-)

The Matrix
February 14th, 2000, 07:46 PM
I made a text program like notepad, you can download the project files and exe in a file from at:
http:\\gamerzpro.virtualave.net\TextWriter.zip