How can i insert some lines at some specific position in a file in vb?
i.e i have a file which has some comments and other data. Every time user will enter a comment it will be appended at the end of comment section of the file.All the data should go down by those number or lines
eg. My file is like
*comment1
*comment2
*comment3
data1
data2
data3
Now if i add a comment comment 4 then it should be modified as follows :
You have to read the whole file in, process it, and then write it back. Best thing you can do is write a very fast processing routine. For large files, it's usually much faster to handle the data as byte arrays and not strings.
Do you have any sample code and a sample of the file you're using?
rite now my application is in testing mode. I'v generated some log i.e the file as :
*2201-sky cloudy
*2202-sky clear
*1102-sky cloudy
*1110-flg and dusty atmoshpere
f12008031015302504 e:\DATA\CCDFilter_SPE_Images\2008\03 f1 e60 x1y 1 place -19.5
f32008031015313104 e:\DATA\CCDFilter_SPE_Images\2008\03 f1 e60 x1y 1 place -19.5
f12008031015314004 e:\DATA\CCDFilter_SPE_Images\2008\03 f1 e60 x1y 1 place -19.5
f32008031015324704 e:\DATA\CCDFilter_SPE_Images\2008\03 f1 e60 x1y 1 Antartica -19.5
f12008031015340004 e:\DATA\CCDFilter_SPE_Images\2008\03 f3 e5 x1y 1 Boston -19.5
f32008031015350804 e:\DATA\CCDFilter_SPE_Images\2008\03 f3 e5 x1y 1 boston -19.5
f12008031015351604 e:\DATA\CCDFilter_SPE_Images\2008\03 f3 e5 x1y 1
Antartica -19.5
f32008031015362404 e:\DATA\CCDFilter_SPE_Images\2008\03 f3 e5 x1y 1 greenland -19.5
f12008031015302504 e:\DATA\CCDFilter_SPE_Images\2008\03 f1 e60 x1y 1 greenland -19.5
f32008031015313104 e:\DATA\CCDFilter_SPE_Images\2008\03 f1 e60 x1y 1 boston -19.5
f12008031015314004 e:\DATA\CCDFilter_SPE_Images\2008\03 f1 e60 x1y 1 boston -19.5
f32008031015324704 e:\DATA\CCDFilter_SPE_Images\2008\03 f1 e60 x1y 1 boston -19.5
f12008031015340004 e:\DATA\CCDFilter_SPE_Images\2008\03 f3 e5 x1y 1 boston -19.5
f32008031015350804 e:\DATA\CCDFilter_SPE_Images\2008\03 f3 e5 x1y 1 boston -19.5
f12008031015351604 e:\DATA\CCDFilter_SPE_Images\2008\03 f3 e5 x1y 1 boston -19.5
f32008031015362404 e:\DATA\CCDFilter_SPE_Images\2008\03 f3 e5 x1y 1 boston -19.5
Writing this log is only one part of the routine. I'v to do tens of other resource consuming tasks too and this processing has to be in one second only.
I'm not sure if INI files will be the kind of format you want but it might be. I think the best option would be to structure the file differently if possible.
I wanted to see what code you're using to write the file, and process/read the file and also how large the file size is on average.
For reading what i hv figured out is use of some two temp files as follows :
Code:
Dim fs As New FileSystemObject
Dim ts1 As TextStream
Dim ts2 As TextStream
Dim ts3 As TextStream
Set ts1 = fs.OpenTextFile(App.Path & "/" & "temp1.txt", ForWriting, True)
Set ts2 = fs.OpenTextFile(App.Path & "/" & "temp2.txt", ForWriting, True)
Set ts3 = fs.OpenTextFile("c:/file1.txt", ForReading, True)
While Not ts3.AtEndOfStream
st = ts3.ReadLine
If Mid(st, 1, 1) = "*" Then
ts2.WriteLine (st)
Else
GoTo a
End If
Wend
a:
While Not ts3.AtEndOfStream
st = ts3.ReadLine
ts1.WriteLine (st)
Wend
ts1.Close
ts2.Close
ts3.Close
Set ts1 = fs.OpenTextFile(App.Path & "/" & "temp1.txt", ForReading, True)
Set ts2 = fs.OpenTextFile(App.Path & "/" & "temp2.txt", ForReading, True)
Set ts3 = fs.OpenTextFile("c:/file1.txt", ForWriting, True)
While Not ts2.AtEndOfStream
st = ts2.ReadAll
ts3.Write (st)
Wend
ts3.Close
Set ts3 = fs.OpenTextFile("c:/file1.txt", ForAppending, True)
While Not ts1.AtEndOfStream
st = ts1.ReadAll
ts3.Write (st)
Wend
Well this is very childish code i think.... i knw there may be a better way to do this....hope some can help..
You might be able to use a UDT (User-Defined Type) array. UDT arrays can be saved and loaded directly from disk without having to parse the file. This makes them extremely fast even for large files and fast for modifying in memory.
I can write an example but I need to know what those values mean to setup the UDT correctly.
This might be a bit slow but it fits the bill in terms of not using any temporary files. Perhaps you can improve on it.
Code:
Private Sub AddNewComment(strNewComment As String)
Dim intFile As Integer
Dim intI As Integer
Dim intJ As Integer
Dim boAdded As Boolean
Dim strData As String
Dim strLines() As String
intFile = FreeFile
'
' Open, read the entire file and Close it
'
Open "C:\tote\Input.txt" For Input As intFile
strData = Input(LOF(intFile), intFile)
Close intFile
'
' Split the data into lines
'
strLines = Split(strData, vbCrLf)
intFile = FreeFile
'
' Open the file, write the comments
' After the last comment has been written, write the new comment
' then copy the rest of the data into the file
'
Open "C:\tote\Input.txt" For Output As intFile
intI = LBound(strLines)
Do
If Mid$(strLines(intI), 1, 1) = "*" Then
Print #intFile, strLines(intI)
Else
Print #intFile, strNewComment
Print #intFile, strLines(intI)
boAdded = True
End If
intI = intI + 1
Loop Until boAdded = True
If boAdded = True Then
For intJ = intI To UBound(strLines)
Print #intFile, strLines(intJ)
Next intJ
End If
Close intFile
End Sub
Use: Call AddNewComment("new Comment")
Strongly suggest you backup the original before trying this as it overwrites the original file.
f32008031015362404 e:\DATA\CCDFilter_SPE_Images\2008\03 f3 e5 x1y 1 boston -19.5
well the format of this string is as follows
>f32008031015362404 is filename
>e:\DATA\CCDFilter_SPE_Images\2008\03- path where file located. It is of the form year and month sub-folders
>f3- f for filter and 3 is filter position
>e5- e for exposure and 5 is 5 sec exposure time
>x1 - x for x dimension binning and 1 is binning ratio
>y1-y for y dimension binning and 1 is binning ration
>boston- place of conducting experiment
>-19.5 -CCD temperature
This is an example, kind of a shot in the dark since I don't know exactly what you're doing but it can be changed.
But it loads/saves real fast, you can modify a log entry really quickly through the UDT array. It saved 436 entries to a file that is only 19KB and loads instantly.
If you need any help, let me know, and you may also want to check out the "Save/load UDT arrays quickly" link in my signature, since that is basically what I'm doing here.
If you need help searching for an entry and modifying any of the values, let me know.