[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:
OPEN "ADD.DAT" FOR INPUT AS#1
DO UNTIL EOF(1)
i% =i% + 1
if i% > ubound(Names$) then redim preserve Names$(1 to i%)
if i% > ubound(goals$) then redim preserve add$(1 to i%)
INPUT #1, Name$(i%), add$(i%)
LOOP
CLOSE #1
Which displays
"Ronaldo","Brazil"
"David","UK"
"Pele","Brazil"
"Mardona","Argentina"
"Doanald","SA"
"Schnider","Germany"
VB Code:
OPEN "Goals.Dat" FOR INPUT AS#1
DO UNTIL EOF(1)
s$=s%+1
if s% > ubound(Names$) then redim preserve Names$(1 to s%)
if s% > ubound(goals$) then redim preserve goals$(1 to s%)
INPUT #1, Names$(i%), goals$(i%)
LOOP
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
Re: Merging 2 Sequential files
give this a try...
VB Code:
Dim LinesAdd() As String
Dim LinesGoal() As String
Dim Final() As String
Dim tmpA() As String
Dim tmpG() As String
Open "ADD.DAT" For Input As #1
LinesAdd = Split(Input(LOF(1), 1), vbCrLf)
Close #1
Open "Goals.DAT" For Input As #1
LinesGoal = Split(Input(LOF(1), 1), vbCrLf)
Close #1
ReDim Final(UBound(LinesAdd))
For x = 0 To UBound(LinesAdd)
Final(x) = LinesAdd(x)
tmpA = Split(LinesAdd(x), ",")
For I = 0 To UBound(LinesGoal)
tmpG = Split(LinesGoal(I), ",")
If tmpG(0) = tmpA(0) Then
Final(x) = Final(x) & "," & tmpG(1)
Exit For
End If
Next
If UBound(tmpA) = 1 Then
Final(x) = Final(x) & "," & Chr(34) & Chr(34)
End If
Next
Open "Final.dat" For Output As #1
For x = 0 To UBound(LinesAdd)
Print #1, Final(x)
Next
Close #1
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
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.
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
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?
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.
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:
Private Sub Form_Load()
LinesAdd
End Sub
then when your form loads it will fix it and create the new Final File with the ""'s
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
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.
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
Re: Merging 2 Sequential files
???? http://www.vbforums.com/
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:
Private Sub FixFinal()
Dim LinesAdd() As String
Dim LinesGoal() As String
Dim Final() As String
Dim tmpA() As String
Dim tmpG() As String
Open "C:\ADD.DAT" For Input As #1
LinesAdd = Split(Input(LOF(1), 1), vbCrLf)
Close #1
Open "C:\Goals.DAT" For Input As #1
LinesGoal = Split(Input(LOF(1), 1), vbCrLf)
Close #1
ReDim Final(UBound(LinesAdd))
For x = 0 To UBound(LinesAdd)
Final(x) = LinesAdd(x)
tmpA = Split(LinesAdd(x), ",")
For I = 0 To UBound(LinesGoal)
tmpG = Split(LinesGoal(I), ",")
If tmpG(0) = tmpA(0) Then
Final(x) = Final(x) & "," & tmpG(1)
Exit For
End If
Next
If UBound(tmpA) = 1 Then
Final(x) = Final(x) & "," & Chr(34) & Chr(34)
End If
Next
Open "C:\Final.dat" For Output As #1
For x = 0 To UBound(LinesAdd)
Print #1, Final(x)
Next
Close #1
End Sub
Private Sub Form_Load()
FixFinal
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
1 Attachment(s)
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
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",""
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
Re: Merging 2 Sequential files
in a module...
VB Code:
Option Explicit
Private Type Players
Name As String
Country As String
Goals As String
End Type
Private Type Goals
Name As String
Goals As String
End Type
Private Sub MergeFiles()
Dim udtPlayer() As Players
Dim udtGolas() As Goals
Dim vSplit As Variant
Dim ff As Integer
Dim lLoop As Long
Dim lRecords As Long
ff = FreeFile
Open "add.dat" For Input As #ff
vSplit = Split(Input(LOF(ff), ff), vbCrLf)
lRecords = UBound(vSplit)
If Len(vSplit(lRecords)) = 0 Then lRecords = lRecords - 1
vSplit = Empty
ReDim udtPlayer(lRecords)
Seek ff, 1
For lLoop = 0 To lRecords
Input #ff, udtPlayer(lLoop).Name, udtPlayer(lLoop).Country
Next lLoop
Close ff
ff = FreeFile
Open "goals.dat" For Input As #ff
vSplit = Split(Input(LOF(ff), ff), vbCrLf)
lRecords = UBound(vSplit)
If Len(vSplit(lRecords)) = 0 Then lRecords = lRecords - 1
vSplit = Empty
ReDim udtGolas(lRecords)
Seek ff, 1
For lLoop = 0 To lRecords
Input #ff, udtGolas(lLoop).Name, udtGolas(lLoop).Goals
Next lLoop
Close ff
For lLoop = 0 To UBound(udtPlayer)
For lRecords = 0 To UBound(udtGolas)
If udtPlayer(lLoop).Name = udtGolas(lRecords).Name Then
udtPlayer(lLoop).Goals = udtGolas(lRecords).Goals
End If
Next lRecords
Next lLoop
ff = FreeFile
Open "final.dat" For Output As #ff
For lLoop = 0 To UBound(udtPlayer)
Write #ff, udtPlayer(lLoop).Name, udtPlayer(lLoop).Country, udtPlayer(lLoop).Goals
Next lLoop
Close ff
End Sub
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....
Re: Merging 2 Sequential files
thanks Agilaz Very Ver Very Much I Think It Is Resolved
Regards Samyo
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
Re: [RESOLVED] Merging 2 Sequential files
thanks :bigyello:
it's not the most brilliant code but nice to hear it works.
hope you enjoy the world cup ;)