|
-
February 14th, 2000, 01:39 PM
#1
Inserting a line in a text file
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
-
February 14th, 2000, 01:59 PM
#2
Re: Inserting a line in a text file
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
[email protected]
The best way to escape a problem, is to solve it.
-
February 14th, 2000, 02:32 PM
#3
Re: Inserting a line in a text file
Absolutely the perfect code...except...instead of being a Private Sub, you might want to try Private Function!! :-)
-
February 14th, 2000, 08:46 PM
#4
Re: Inserting a line in a text file
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|