Results 1 to 10 of 10

Thread: File read & write problem..pls help me..

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2010
    Posts
    20

    File read & write problem..pls help me..

    Hello,

    I have a file called infile.txt where there are some datas. After so many lines again same data repeats with having different value.

    I want to search value for particular parameter calledSubscriberIMSI & throughout the file where this value matches for calledSubscriberIMSI, it will take all the datas & produce it to a output file called outfile.txt.

    Sample values are given below...

    UMTSGSMPLMNCallDataRecord
    mSTerminatingSMSinMSC
    dateForStartOfCharge 07020D'H
    exchangeIdentity "BBSR_MSC_R11"'S
    recordSequenceNumber 25510'D
    timeForStartOfCharge 17 2 44'BCD
    outputType 04'H
    switchIdentity 0001'H
    callIdentificationNumber 54292'D
    chargedParty 00'H
    outgoingRoute "OBBKJRO"'S
    calledSubscriberIMEI 358364004355120F'TBCD
    calledSubscriberIMSI 404281130154600F'TBCD
    firstCalledLocationInformation 04F48239DB7157'H
    ->2|0|63 09'H
    calledPartyNumber 11919853313214'TBCD
    frequencyBandSupported 00'H
    teleServiceCode 21'H
    mSCIdentification 11919853099002'TBCD
    lastCalledLocationInformation 04F48239DB7157'H
    serviceCentreAddress 11919854099060'TBCD
    tAC 04020B'H
    originForCharging 00'H
    messageTypeIndicator 00'H
    originatingAddress 0E1C4A47854620'TBCD


    UMTSGSMPLMNCallDataRecord
    mSTerminatingSMSinMSC
    dateForStartOfCharge 07020D'H
    exchangeIdentity "BBSR_MSC_R11"'S
    recordSequenceNumber 25511'D
    timeForStartOfCharge 17 2 44'BCD
    outputType 04'H
    switchIdentity 0001'H
    callIdentificationNumber 54293'D
    chargedParty 00'H
    outgoingRoute "OBBCUTO"'S
    calledSubscriberIMEI 357088005862520F'TBCD
    calledSubscriberIMSI 404281130154601F'TBCD
    firstCalledLocationInformation 04F48237847937'H
    ->2|0|63 09'H
    calledPartyNumber 11919853313210'TBCD
    frequencyBandSupported 00'H
    teleServiceCode 21'H
    mSCIdentification 11919853099002'TBCD
    lastCalledLocationInformation 04F48237847937'H
    serviceCentreAddress 11919854099060'TBCD
    tAC 04020B'H
    originForCharging 00'H
    messageTypeIndicator 00'H
    originatingAddress 0E1C4A47854620'TBCD


    UMTSGSMPLMNCallDataRecord
    mSTerminatingSMSinMSC
    dateForStartOfCharge 07020D'H
    exchangeIdentity "BBSR_MSC_R11"'S
    recordSequenceNumber 25512'D
    timeForStartOfCharge 17 2 44'BCD
    outputType 04'H
    switchIdentity 0001'H
    callIdentificationNumber 54308'D
    chargedParty 00'H
    outgoingRoute "OBBBHUO"'S
    calledSubscriberIMEI 351119608143270F'TBCD
    calledSubscriberIMSI 404281130154600F'TBCD
    firstCalledLocationInformation 04F482371F9DDD'H
    ->2|0|63 09'H
    calledPartyNumber 11919853313209'TBCD
    frequencyBandSupported 00'H
    teleServiceCode 21'H
    mSCIdentification 11919853099002'TBCD
    lastCalledLocationInformation 04F482371F9DDD'H
    serviceCentreAddress 11919854099060'TBCD
    tAC 04020B'H
    originForCharging 00'H
    messageTypeIndicator 00'H
    originatingAddress 0E1C4A47854620'TBCD

    Pls note that for each record, UMTSGSMPLMNCallDataRecord is common which i have made in bold & again starts another record. Now, I want to search data 404281130154600 for calledSubscriberIMSI . In this case it has ben found twice. so my program should take the whole segment i.e from UMTSGSMPLMNCallDataRecord to originatingAddress for 1st occurance & again from UMTSGSMPLMNCallDataRecord to originatingAddress for 2nd ocuurance & put it to a file outfile.txt.

    pls help me to solve the issue..I have attached an input file infile.txt

    Thnkx & Regards
    Sajib
    Attached Files Attached Files

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: File read & write problem..pls help me..

    you can try like
    vb Code:
    1. Dim file2() As String
    2. d = vbNewLine & vbNewLine & vbNewLine
    3. f = "calledSubscriberIMSI"
    4. v = "404281130154605F"
    5. Open "c:\temp\infile.txt" For Input As 1
    6. file1 = Split(Input(LOF(1), #1), d)
    7. Close 1
    8. ReDim file2(UBound(file1))
    9. j = 0
    10. For i = 0 To UBound(file1)
    11.     pos = 0
    12.     pos = InStr(pos + 1, file1(i), f) + Len(f) + 40
    13.     If Mid(file1(i), pos, Len(v)) = v Then
    14.         file2(j) = file1(i)
    15.         j = j + 1
    16.     End If
    17. Next
    18. ReDim Preserve file2(j - 1)
    19. Open "c:\temp\outfile.txt" For Output As 1
    20. Print #1, Join(file2, d)
    21. Close 1
    change paths and v to suit
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2010
    Posts
    20

    Re: File read & write problem..pls help me..

    Thnkx westconn1 for the post.
    But whenever I run it with a big input file it's showing "Subscript out of range" on the line ReDim Preserve file2(j - 1)..

    Is there any way to cope up with this?

    Thnkx

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: File read & write problem..pls help me..

    What is the value of j when you get that error? Zero maybe? If so, your search failed to find any matches. You should check to see if j > 0 before the final ReDim & possibly outputing a blank file.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2010
    Posts
    20

    Re: File read & write problem..pls help me..

    ya it's giving a blank file..
    can u pls modify the code for a big input file?

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: File read & write problem..pls help me..

    It's not the file size. Your search routine is not working so you need to work on that. If anything was found, j would not be zero.
    The file isn't > 2gb is it?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Aug 2010
    Posts
    20

    Re: File read & write problem..pls help me..

    ya in this case j is becoming zero after search..pls tell me how to modify the above code?

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: File read & write problem..pls help me..

    Here's a slight twist on westconn's idea. I've commented the code and expect you to examine it to answer your own questions. If you have additional questions, ensure you identify the problem & what line(s) of code that are causing the problems.
    Code:
    Private Function FindData(FileName As String, FieldName As String, Criteria As String)
    
    Dim file1 As String, outList As String
    Dim lHeader As Long
    Dim lPtr As Long, lStopPtr As Long
    
    Const sHeader As String = "UMTSGSMPLMNCallDataRecord"
    
    ' add error trapping here so if you pass bad filename or error opening file, you'll know
    lStopPtr = FreeFile()
    Open FileName For Input As #lStopPtr
    file1 = Input(LOF(1), #lStopPtr)
    Close #lStopPtr
    If Len(file1) = 0 Then Exit Function
    
    Do
        ' find next occurrence of the FieldName
        lPtr = InStr(lPtr + 1, file1, FieldName)
        If lPtr = 0 Then Exit Do ' if not found, done searching file
        
        ' found field name see if record matches criteria
        ' determine where end of line stops, which should be a carriage return
        lStopPtr = InStr(lPtr + 1, file1, vbCrLf)
        If lStopPtr = 0 Then lStopPtr = Len(file1) + 1
        
        ' now search just that part of the file, extracting a line to test
        If InStr(Mid$(file1, lPtr + Len(FieldName), lStopPtr - lPtr), Criteria) Then
            'found it
            ' get the header start
            lPtr = InStrRev(file1, sHeader, lPtr)
            If lPtr = 0 Then lPtr = 1
            ' get the next header
            lStopPtr = InStr(lStopPtr, file1, sHeader)
            If lStopPtr = 0 Then lStopPtr = Len(file1) + 1
            ' extract stuff between headers
            outList = outList & Mid$(file1, lPtr, lStopPtr - lPtr)
        Else
            ' do nothing. didn't find a match in that field
        End If
        lPtr = lStopPtr ' move the pointer along
    Loop
    
    FindData = outList ' return the result
        
    End Function
    
    ' example usage of the above function
    Private Sub Command1_Click()
    
    Dim sResults As String
    
    ' change file name, field name & criteria as needed. Can use variables vs hardcoding the parameters
    sResults = FindData("c:\infile[1].txt", "calledSubscriberIMSI", "404281130154600")
    If Len(sResults) Then
        MsgBox "Found"
        ' open a file and write sResults to it
    Else
        MsgBox "Not found"
    End If
    
    End Sub
    Edited: If you have questions about some of the VB functions (InStr, InStrRev, Mid, etc), open your help files.
    Here's an on-line VB help file, but not exactly the easiest to use
    Last edited by LaVolpe; Sep 1st, 2010 at 10:31 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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

    Re: File read & write problem..pls help me..

    Isn't this a duplicate of this http://www.vbforums.com/showthread.php?t=626102 albeit with significant scope creep from your initial request.

    Dilettante has already given you a complete, compact and flexible solution, all you have to do is to change the code from writing the results into a TextBox to writing them to a file - something you should be able to do for yourself.

    Looking at Dilettante's code, it's easy to see where the results are added to the textbox and if you look at the code I posted you can see how to open and write data to a file. In addition, there are plenty of examples / tutorials on file input and output here.

    So far I've seen no code that you have written or attempted to write in order to help yourself.

    BTW I don't respond to PMs asking for help, I only respond to public requests posted to the forum.
    Last edited by Doogle; Sep 2nd, 2010 at 02:47 AM.

  10. #10
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: File read & write problem..pls help me..

    Quote Originally Posted by Doogle View Post
    Isn't this a duplicate of this http://www.vbforums.com/showthread.php?t=626102 albeit with significant scope creep from your initial request.

    ...

    So far I've seen no code that you have written or attempted to write in order to help yourself.
    Really? Wish I would've seen/noticed that post beforehand!
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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