Results 1 to 11 of 11

Thread: Insert in file

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    24

    Insert in file

    Hello all,

    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 :

    *comment1
    *comment2
    *comment3
    *comment4
    data1
    data2
    data3

    I can do this with the use of two temporary files. But the file is damn large and this thing is damn tedious.

    Can ne one help me out with some other way of doing this?

    Thanks & Regards.

  2. #2
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Insert in file

    what about using an ini file ?
    so can write directly under sections.

  3. #3
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Insert in file

    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?

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    24

    Re: Insert in file

    @FAZI

    I dont know much abt ini files. I'll see to it.

    @digirev

    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.

    What code u asked for?

    regards.

  5. #5
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Insert in file

    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.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    24

    Re: Insert in file

    well, the code is deep buried in many loops...i'll try to merge it .....

    For writing file i use something like :

    Code:
    Set ts = fs.OpenTextFile(schedule_path & "/" & yr & "/" & "log" & catalog_path & ".txt", ForAppending, True)
    
    ts.Write (spe_file_name)
                        ts.Write (" ")
                        ts.Write (tmp)
                        ts.Write (" ")
                        ts.Write ("f" & Filter_exposure(I, 0) & " e")
                        If Filter_exposure(I, 2) = 1 Then
                            ts.Write (Filter_exposure(I, 1) / 1000000)
                        ElseIf Filter_exposure(I, 2) = 2 Then
                            ts.Write (Filter_exposure(I, 1) / 1000)
                        ElseIf Filter_exposure(I, 2) = 3 Then
                            ts.Write (Filter_exposure(I, 1))
                        Else
                            ts.Write (Filter_exposure(I, 1) * 60)
                        End If
                        ts.Write (" ")
                        ts.Write ("x" & bin_x & "y" & bin_y)
                        ts.Write (" ")
                        ts.Write (prof_det(2))
                        ts.Write (" ")
                        ts.WriteLine (prof_det(0))
    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..

    Thx..

  7. #7
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Insert in file

    Ok, what are these values?

    03 f3 e5 x1y 1 boston -19.5

    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.

  8. #8
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Insert in file

    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.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    24

    Re: Insert in file

    @digirev

    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

    Well as such u can hve everything as string

    @ doogle

    let me try out the code.....

    thx..

  10. #10
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Insert in file

    Did Doogle's example work?

    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.
    Attached Files Attached Files

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    24

    Resolved Re: Insert in file

    thanks digirev and doogle......

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width