Results 1 to 20 of 20

Thread: [RESOLVED] Merging 2 Sequential files

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    56

    Resolved [RESOLVED] Merging 2 Sequential files

    Hello

    Pl Help me with this simple solution
    I want to merge two files How can i do it ?

    VB Code:
    1. OPEN "ADD.DAT" FOR INPUT AS#1
    2. DO UNTIL EOF(1)
    3. i% =i% + 1
    4. if i% > ubound(Names$) then redim preserve Names$(1 to i%)
    5. if i% > ubound(goals$) then redim preserve add$(1 to i%)
    6.  
    7. INPUT #1, Name$(i%), add$(i%)
    8. LOOP
    9. CLOSE #1

    Which displays
    "Ronaldo","Brazil"
    "David","UK"
    "Pele","Brazil"
    "Mardona","Argentina"
    "Doanald","SA"
    "Schnider","Germany"


    VB Code:
    1. OPEN "Goals.Dat" FOR INPUT AS#1
    2. DO UNTIL EOF(1)
    3. s$=s%+1
    4. if s% > ubound(Names$) then redim preserve Names$(1 to s%)
    5. if s% > ubound(goals$) then redim preserve goals$(1 to s%)
    6. INPUT #1, Names$(i%), goals$(i%)
    7. LOOP
    8. CLOSE #1

    Which displays
    "Ronaldo","10"
    "David","4"
    "Doanald","3"
    "Schnider","2"

    I want to merge above two files and get the result as
    "Ronaldo","Brazil","10"
    "David","UK","4"
    "Pele","Brazil",""
    "Mardona","Argentina",""
    "Doanald","SA","3"
    "Schnider","Germany","2"

    Pl Help

    Regards

    Samyo





    i% = 0

    for i% = 0 to ubound(Name$)
    FullGreeting$(i%) = Greeting$(i%) & ", " & Name$(i%)
    next i

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Merging 2 Sequential files

    give this a try...
    VB Code:
    1. Dim LinesAdd() As String
    2.     Dim LinesGoal() As String
    3.     Dim Final() As String
    4.     Dim tmpA() As String
    5.     Dim tmpG() As String
    6.     Open "ADD.DAT" For Input As #1
    7.         LinesAdd = Split(Input(LOF(1), 1), vbCrLf)
    8.     Close #1
    9.     Open "Goals.DAT" For Input As #1
    10.         LinesGoal = Split(Input(LOF(1), 1), vbCrLf)
    11.     Close #1
    12.     ReDim Final(UBound(LinesAdd))
    13.    
    14.     For x = 0 To UBound(LinesAdd)
    15.         Final(x) = LinesAdd(x)
    16.         tmpA = Split(LinesAdd(x), ",")
    17.         For I = 0 To UBound(LinesGoal)
    18.             tmpG = Split(LinesGoal(I), ",")
    19.             If tmpG(0) = tmpA(0) Then
    20.                 Final(x) = Final(x) & "," & tmpG(1)
    21.                 Exit For
    22.             End If
    23.         Next
    24.         If UBound(tmpA) = 1 Then
    25.             Final(x) = Final(x) & "," & Chr(34) & Chr(34)
    26.         End If
    27.     Next
    28.    
    29.     Open "Final.dat" For Output As #1
    30.         For x = 0 To UBound(LinesAdd)
    31.             Print #1, Final(x)
    32.         Next
    33.     Close #1
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    56

    Re: Merging 2 Sequential files

    Thanks

    But why dont you submit the code accordingly as i've wrriten the code. Without using split functions

    Reagrds

    Samyo

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Merging 2 Sequential files

    What is wrong with using Split?

    See my post your duplicate thread (now closed - as we dont like duplicate threads) for an explanation of what you could do.

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    56

    Re: Merging 2 Sequential files

    Thanks to you and Static


    ADD.DAT files

    I've used Split function
    Which displays
    "Ronaldo","Brazil"
    "David","UK"
    "Pele","Brazil"
    "Mardona","Argentina"
    "Doanald","SA"
    "Schnider","Germany"
    ====Suppose Here i add Two More recs. in Add.Dat file
    "Mohamed","Saudi Arabia"
    "Richard","USA"

    Goals.Dat File I let it remain same
    "Ronaldo","10"
    "David","4"
    "Doanald","3"
    "Schnider","2"

    What happens i GET the result as follows using the code given by Static
    "David","UK","4"
    "Doanald","SA","3"
    "Mardona","Argentina",""
    "Mohamed","Saudi Arabia"
    "Pele","Brazil",""
    "Richard","USA"
    "Ronaldo","Brazil","10"
    "Schnider","Germany","2"



    Now I want the result as follows using Split given by Static
    By merging above two files and get The specific result as Follows
    "David","UK","4"
    "Doanald","SA","3"
    "Mardona","Argentina",""
    "Mohamed","Saudi Arabia",""
    "Pele","Brazil",""
    "Richard","USA",""
    "Ronaldo","Brazil","10"
    "Schnider","Germany","2"

    Pl help me to modify the Code Given by Static

    Regards
    Samyo

  6. #6
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Merging 2 Sequential files

    are you manually adding these to the file?

    the code I gave should add the "" for the players with no score??
    it does with Mardona & Pele??

    are u reading the "final" before running the add lines code?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    56

    Re: Merging 2 Sequential files

    Hello Static
    Yes At Present I am manually puting the data.
    What difference does it make if i put the data manually directly in the file
    or Later develop code with where from Two text box i can put the data.

    No Matter If Ubound increases in File Add.dat Your Routine should work
    What happens is that it in Add.Dat Ubound(LinesAdd) = 8
    Where as Ubound(LinesGoal) = 4 JUST SEE THE DATA
    I've just put goal of each players with's it name and their goals
    those are only displayed in the "Players.dayt"but why not the players name also with blank goals.
    Thats where i want to merge file Players where it is combined.

    Are u telling me that ubound in both files should be equal ?

    Do i change the names$ in goal.dat to Lnames$ ?

    What do u mean by reading Final before adding lines?
    or How do I read Final before adding linesa .Pl specify

    Regards

    Samyo.

  8. #8
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Merging 2 Sequential files

    no the ubound does not need to equal.. as long as ADD.DAT is longer or equal to Goals

    after u add the names manually... then u need to run the LinesAdd Funtion so it can add the ""

    in fact.. do this

    VB Code:
    1. Private Sub Form_Load()
    2.     LinesAdd
    3. End Sub
    then when your form loads it will fix it and create the new Final File with the ""'s
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  9. #9

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    56

    Re: Merging 2 Sequential files

    Hello Static

    I really dont understand what have you send in the Form load
    something is missing in the form load
    Pl tell where is the LinesAdd function in your code which you sent me

    Reagrds

    Samyo

  10. #10
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Merging 2 Sequential files

    in post #2 (http://www.vbforums.com/showpost.php...79&postcount=2)
    I posted the function that is being called....

    basically.. in the form load, you'll want to run through the code that creates the Final.dat file. which is the code I posted.
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  11. #11

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    56

    Re: Merging 2 Sequential files

    Sorry Static
    I AM UNNECESSARY BOTHERING YOU

    WHETHER ON FORM-LOAD OR CMD1_CLICK
    STILL I AM FACING THE SAME

    WHY DONT YOU TRY THE RESULT FOR YOUR SELF

    Pl see the following result

    What happens i GET the result as follows using the "David","UK","4"----- see the the 3 Variables
    "Doanald","SA","3"------ see the the 3 Variables
    "Mardona","Argentina",""----see the 3 variables
    Goals=0
    "Mohamed","Saudi Arabia",----see the 2 variables
    AS NO RECORDS IN
    GOALS.DAT

    "Pele","Brazil",""---------see the 3 variables
    Goals=0
    "Richard","USA"---------SEE ONLY 2 VARIABLES
    AS NO RECORDS IN
    GOALS.DAT

    "Ronaldo","Brazil","10"-------see the 3 variables
    "Schnider","Germany","2"-------see the 3 variables



    Now I want the result as follows using Split given by Static
    IT SHOULD CONTAIN THREE VARIABLES
    OTHERWISE I WILL GET ERROR INPUT PAST END
    IF I WANT TO READ FILE

    "David","UK","4"
    "Doanald","SA","3"
    "Mardona","Argentina",""
    "Mohamed","Saudi Arabia",""
    "Pele","Brazil",""
    "Richard","USA",""
    "Ronaldo","Brazil","10"
    "Schnider","Germany","2"

    Pl help me to modify the Code Given by Static

    Regards
    Samyo

  12. #12
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Merging 2 Sequential files

    ????

    I dont understand.. the code I gave you should work in either case!??

    here I just create the 2 files and ran this:
    VB Code:
    1. Private Sub FixFinal()
    2.     Dim LinesAdd() As String
    3.     Dim LinesGoal() As String
    4.     Dim Final() As String
    5.     Dim tmpA() As String
    6.     Dim tmpG() As String
    7.     Open "C:\ADD.DAT" For Input As #1
    8.         LinesAdd = Split(Input(LOF(1), 1), vbCrLf)
    9.     Close #1
    10.     Open "C:\Goals.DAT" For Input As #1
    11.         LinesGoal = Split(Input(LOF(1), 1), vbCrLf)
    12.     Close #1
    13.     ReDim Final(UBound(LinesAdd))
    14.    
    15.     For x = 0 To UBound(LinesAdd)
    16.         Final(x) = LinesAdd(x)
    17.         tmpA = Split(LinesAdd(x), ",")
    18.         For I = 0 To UBound(LinesGoal)
    19.             tmpG = Split(LinesGoal(I), ",")
    20.             If tmpG(0) = tmpA(0) Then
    21.                 Final(x) = Final(x) & "," & tmpG(1)
    22.                 Exit For
    23.             End If
    24.         Next
    25.         If UBound(tmpA) = 1 Then
    26.             Final(x) = Final(x) & "," & Chr(34) & Chr(34)
    27.         End If
    28.     Next
    29.    
    30.     Open "C:\Final.dat" For Output As #1
    31.         For x = 0 To UBound(LinesAdd)
    32.             Print #1, Final(x)
    33.         Next
    34.     Close #1
    35. End Sub
    36. Private Sub Form_Load()
    37.     FixFinal
    38. End Sub
    and it worked fine!???

    I opened c:\Final.dat and it had all the parts...
    Zip your project and attach it.. let me take a look at it
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  13. #13

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    56

    Re: Merging 2 Sequential files

    For this two records only iam able to have three variables
    for others four variables

    Sending my merging2.zip
    and all the dat files Pl Open.dat files with notepad
    Pl see

    Regards

    Samyo
    Attached Files Attached Files

  14. #14
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Merging 2 Sequential files

    ?? im looking at Final.dat and it has 3 variables in it...
    "Ronaldo","Brazil","10",""
    "David","UK","4",""
    "Mohamed","Saudi Arabia",""
    "Pele","Brazil","",""
    "Mardona","Argentina","",""
    "Doanald","SA","3",""
    "Schnider","Germany","2",""
    "Richard","USA",""
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  15. #15

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    56

    Re: Merging 2 Sequential files

    DEAR STATIC
    HOW IT TAKES 3 VARAIBLES I THINK YOU AR FORGETING ,""
    OR JUST COUNT DELIMETERS OR COMMAS
    records
    "Mohamed","Saudi Arabia",""
    1 , 2 , 3 ----> PRINTS THREE VARIABLES , 2 COMMAS
    "Richard","USA",""
    1 , 2 , 3----> PRINTS THREE VARIABLES , 2 COMMAS
    "Ronaldo","Brazil","10",""
    1 , 2 , 3 , 4 PRINTS FOUR VARIABLES , 3 COMMAS
    "David","UK","4",""
    AND OTHER RECORDS PRINTS FOUR VARIABLES, 3 COMMAS

    WHEN WE MERGE I WANT ALL RECORDS WITH FOUR VARIABLES AND THREE COMMAS

    REGARDS
    SAMYO

  16. #16
    Lively Member Agilaz's Avatar
    Join Date
    Jun 2006
    Posts
    98

    Re: Merging 2 Sequential files

    in a module...

    VB Code:
    1. Option Explicit
    2.  
    3. Private Type Players
    4.     Name As String
    5.     Country As String
    6.     Goals As String
    7. End Type
    8.  
    9. Private Type Goals
    10.     Name As String
    11.     Goals As String
    12. End Type
    13.  
    14. Private Sub MergeFiles()
    15.     Dim udtPlayer() As Players
    16.     Dim udtGolas() As Goals
    17.     Dim vSplit As Variant
    18.     Dim ff As Integer
    19.     Dim lLoop As Long
    20.     Dim lRecords As Long
    21.    
    22.     ff = FreeFile
    23.     Open "add.dat" For Input As #ff
    24.     vSplit = Split(Input(LOF(ff), ff), vbCrLf)
    25.     lRecords = UBound(vSplit)
    26.     If Len(vSplit(lRecords)) = 0 Then lRecords = lRecords - 1
    27.     vSplit = Empty
    28.     ReDim udtPlayer(lRecords)
    29.     Seek ff, 1
    30.     For lLoop = 0 To lRecords
    31.         Input #ff, udtPlayer(lLoop).Name, udtPlayer(lLoop).Country
    32.     Next lLoop
    33.     Close ff
    34.    
    35.     ff = FreeFile
    36.     Open "goals.dat" For Input As #ff
    37.     vSplit = Split(Input(LOF(ff), ff), vbCrLf)
    38.     lRecords = UBound(vSplit)
    39.     If Len(vSplit(lRecords)) = 0 Then lRecords = lRecords - 1
    40.     vSplit = Empty
    41.     ReDim udtGolas(lRecords)
    42.     Seek ff, 1
    43.     For lLoop = 0 To lRecords
    44.         Input #ff, udtGolas(lLoop).Name, udtGolas(lLoop).Goals
    45.     Next lLoop
    46.     Close ff
    47.    
    48.     For lLoop = 0 To UBound(udtPlayer)
    49.         For lRecords = 0 To UBound(udtGolas)
    50.             If udtPlayer(lLoop).Name = udtGolas(lRecords).Name Then
    51.                 udtPlayer(lLoop).Goals = udtGolas(lRecords).Goals
    52.             End If
    53.         Next lRecords
    54.     Next lLoop
    55.    
    56.     ff = FreeFile
    57.     Open "final.dat" For Output As #ff
    58.     For lLoop = 0 To UBound(udtPlayer)
    59.         Write #ff, udtPlayer(lLoop).Name, udtPlayer(lLoop).Country, udtPlayer(lLoop).Goals
    60.     Next lLoop
    61.     Close ff
    62. End Sub

  17. #17
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Merging 2 Sequential files

    samyo!! you're killin me!

    originally u wanted 3 variables!??? now 4?

    take a look at Agilaz code, good idea to use user types for the data....
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  18. #18

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    56

    Re: Merging 2 Sequential files

    thanks Agilaz Very Ver Very Much I Think It Is Resolved

    Regards Samyo

  19. #19

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    56

    Re: Merging 2 Sequential files

    Quote Originally Posted by samyo
    thanks Agilaz Very Ver Very Much I Think It Is Resolved

    Regards Samyo

    I FEEL THAT CODE POSTED BY AGILAZ SHOULD BE DISPLAYED ON ALL VB USERS WEBSITES

  20. #20
    Lively Member Agilaz's Avatar
    Join Date
    Jun 2006
    Posts
    98

    Re: [RESOLVED] Merging 2 Sequential files

    thanks

    it's not the most brilliant code but nice to hear it works.

    hope you enjoy the world cup

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