Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Text File question

Hi all,

i have the following code:

path = "C:\testfile\testfile.txt"

Open path For Append As #1
Write #1,
Write #1, "test";
Close #1

Mr Alan
Mr Happy
Mr Grumpy

test

this basicall prints a blank line and then 'test' at the bottom of the file as you can see above.

How could i tell it to write 'test' at the top of the page, so it would move the data already there down 1 'enter' i.e.

test

Mr Alan
Mr Happy
Mr Grumpy
[601 byte] By [mp_direct] at [2007-11-11 7:45:19]
# 1 Re: Text File question
You will need to read the entire file in and then re-write after you have written "test". Code not tested, but should give you the idea.

Dim MyString() As String 'Create string array
Dim Counter As Integer

Path = "C:\testfile\testfile.txt"

Open Path For Input As #1
While Not EOF(1)
ReDim Preserve MyString(Counter)
Input #1, MyString(Counter)
Counter = Counter + 1
Wend
Close #1

Open Path For Output As #1
Write #1, "test"
For Counter = 0 To UBound(MyString) - 1
Write #1, MyString(Counter)
Next
Close #1

Steve. :cool:
Steve Grant at 2007-11-11 17:26:29 >
# 2 Re: Text File question
If the input file is small, here's a simpler/faster method to read a text file into a string array:

Dim hFile As Integer
Dim sBuffer As String
Dim sText() As String

hFile = FreeFile
Open FileName For Input As hFile
sBuffer = Input$(LOF(hFile), hFile)
Close hFile

sText = Split(sBuffer, vbCrLf)
Phil Weber at 2007-11-11 17:27:26 >
# 3 Re: Text File question
Following on Phil's example, you could do this:

sBuffer = "new line of text" & sBuffer

Then open the original file for output, and write (or print) sBuffer.

Now you have the value at the front.
edburdo at 2007-11-11 17:28:35 >