Skupiny Google už nepodporují nová předplatná ani příspěvky Usenet. Historický obsah lze zobrazit stále.

Printing to a file

1 zobrazení
Přeskočit na první nepřečtenou zprávu

Veeteq

nepřečteno,
11. 7. 2000 3:00:0011.07.00
komu:
Hi

I repeat my question about printing something to a file in a defined
position.

The problem is:

get i.e. 6th line of text from file
modify the contents of the line
write it back to the same position

does anybody know how to do that.

Veeteq

Stratos Malasiotis

nepřečteno,
11. 7. 2000 3:00:0011.07.00
komu: Veeteq
Hi Veeteq,

I think you asked the same thing yesterday and received some nice
pointers.
The reason that we didn't give you a precise answer is because this is
an easy thing that most people can do with a bit of effort.
Forgive me for becoming critical but, this is not a place two issue
orders and receive free services. There is a saying in the place that I
come from that says: "He doesn't only expect to give him free food , he
wants it chewed as well." (they say it for lazy people). If you
continue doing that you'll not be receiving much help in the future.

Anyway... forget that.
Here is a function that does what you want. There are various ways to
do it but I haven't see that before so I suggest it as a different
alternative option.


--------------------------------------------------------------------------
Option Explicit

Public Function fncReplaceTextLine(TextFileLocation As String, _
LineNumber As Long, _
Optional NewLineString As String =
vbNullString) As Boolean
'replaces the specified line of a text document with a new string
Dim File_hWnd As Long
Dim LineStringCol As New Collection
Dim OldLineStr As String, NewFileStr As Variant
Dim LineCounter As Long
fncReplaceTextLine = False
OldLineStr = vbNullString
LineCounter = 0
On Error GoTo ExitFunction
'validate file existance:
If Not Len(Dir(TextFileLocation)) > 0 Then
Exit Function
End If
'get a handle to a new file:
File_hWnd = FreeFile
'open the specified file for sequential access:
Open TextFileLocation For Input Access Read Shared As File_hWnd
'get the contents of the file line by Line:
Do Until EOF(File_hWnd)
LineCounter = LineCounter + 1
Line Input #File_hWnd, OldLineStr
'replace the requested line
If LineCounter = LineNumber Then
OldLineStr = NewLineString
End If
'put in the collection
LineStringCol.Add OldLineStr, CStr(LineCounter)
Loop
'release the file handle:
Close File_hWnd
'reopen the same file for Output
Open TextFileLocation For Output Access Write As File_hWnd
'print the new file string from the collection in the file
For Each NewFileStr In LineStringCol
Print #File_hWnd, NewFileStr
Next NewFileStr
'release the file handle:
Close File_hWnd
'empty the collection object
Set LineStringCol = Nothing
'the function has been completed succesfully
fncReplaceTextLine = True
Exit Function
'error handling:
ExitFunction:
Close File_hWnd
End Function


Sub test_fncReplaceTextLine()
Debug.Print fncReplaceTextLine(TextFileLocation:="D:\temptext.txt",
_
LineNumber:=15, _
NewLineString:="This is a new line")
End Sub

--------------------------------------------------------------------------

HTH
Stratos

Myrna Larson

nepřečteno,
11. 7. 2000 3:00:0011.07.00
komu:
On Tue, 11 Jul 2000 08:14:07 GMT, "Veeteq" <vee...@klub.chip.com.pl> wrote:

>I repeat my question about printing something to a file in a defined
>position.
>
>The problem is:
>
>get i.e. 6th line of text from file
>modify the contents of the line
>write it back to the same position
>
>does anybody know how to do that.

As you were told yesterday, unless all of the lines are exactly the same
length, there is no way to do what you describe, i.e. retrieve line 6, modify
it, and write it back.

Stratos has given you some code for one way to handle the situtation when the
file does not have same-length records.

If the file is small enough to fit into a single string, and the new line is
longer than or the same length as the old one, you could also do it as below.
The problem with this is that if the new line is shorter than the old, you'll
have some garbage at the end of the file. The only way to eliminate that is to
write to a 2nd file instead of overwriting the original, or use sequential
input and output to 2 files.

Sub ReplaceFileLine(FileName As String, LineNum As Long, NewLine As String)
Dim FileNum As Long
Dim sText As String, sTemp As String
Dim p As Long

If Right$(NewLine, 2) <> vbCrLf Then
NewLine = NewLine & vbCrLf
End If

FileNum = FreeFile()
Open FileName For Binary As #FileNum
sText = Space$(LOF(FileNum))
Get #FileNum, 1, sText

p = 0
For i = 1 To LineNum - 1
p = InStr(p + 1, sText, vbCrLf)
Next i

sTemp = Left$(sText, p + 1)
Put #FileNum, 1, sTemp
Put #FileNum, , NewLine

p = InStr(p + 1, sText, vbCrLf)
sTemp = Mid$(sText, p + 2)
Put #FileNum, , sTemp
Close #FileNum
End Sub

Rob

nepřečteno,
13. 7. 2000 3:00:0013.07.00
komu:
use a batch file with echo, head, tail. There are Win32 versions of these
command line utils.

Cheers...


"Veeteq" <vee...@klub.chip.com.pl> wrote in message
news:01bfeb0f$89f47780$8900000a@veeteq...
> Hi


>
> I repeat my question about printing something to a file in a defined
> position.
>
> The problem is:
>
> get i.e. 6th line of text from file
> modify the contents of the line
> write it back to the same position
>
> does anybody know how to do that.
>

> Veeteq

0 nových zpráv