7 Replies - 910 Views - Last Post: 19 August 2009 - 06:33 PM Rate Topic: -----

#1 kayatri  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 216
  • Joined: 05-May 09

Reading,Comparing and writing to text file

Post icon  Posted 17 August 2009 - 11:32 PM

I have a 2 text files

Text file 1:
.ERR - Max6 Error file

[2 19.40 44.7] X6.950 Y-4.705 106.522, 222.048
Open -106.522, 222.048


[2 05.52 39.6] X7.720 Y5.010 214.550, 205.048, 101.078
Open -214.550, 101.078


[2 07.69 39.2] X7.786 Y3.489 215.574, 214.075, 204.067
Short +202.059, 204.040, 204.539, 205.539, 205.540, 207.039, 208.039, 207.539, 207.540, 208.059, 208.060

Text file 2

------------------------------------------------------------------------------
AGILENT ICT FIXTURE TRACE REPORT Thu Jul 30 23:13:56 2009

D:/Jobs/Emerson/thm1340/atca7105/fixture/trace
------------------------------------------------------------------------------

Fixture Type : Agilent SimPlate Express
Fixture Size : Full
Fixture Part Number : 44200L
Top Probes Allowed : Enabled
Autofile : 100
Units : English
Wiring Method : Automatic
-----------------------------------------------------------------------------
( Pin ) [ Probe ] Length = in.
-----------------------------------------------------------------------------
Node Name "Node Name"
Probe[Top] [b rr.rr cc.c] On Device.Pin
Alternate On Device.Pin
Wire Color: "Color" Gauge: "Gauge"
Path (b rr.rr cc.c) <Length> (b rr.rr cc.c) <Length> [b rr.rr cc.c]
Use "Use"

-----------------------------------------------------------------------------
Node Name 3N210
Top [2 11.13 -00.9]
Wire Color: Blue Gauge: 30
Path (2 03.43 -05.7) <6.0> [2 11.13 -00.9]
Use Transfer

Path (1 00.50 53.0) <6.5> (2 03.43 -05.7) <6.0> [2 11.13 -00.9]
Use Shorts Test ASRU Source Analog Guard

Path (1 01.00 52.0) <6.5> (2 03.43 -05.7) <6.0> [2 11.13 -00.9]
Use G Bus Sense


-----------------------------------------------------------------------------
Node Name MEM_B_DQ6_R
Probe [2 11.00 32.7]
Wire Color: Blue Gauge: 30
Path (2 11.50 67.0) <5.5> [2 11.00 32.7]
Use ASRU Detector Shorts Test

Path (2 10.50 67.0) <1.0> (2 11.50 67.0) <5.5> [2 11.00 32.7]
Use I Bus Sense

-----------------------------------------------------------------------------
Node Name MEM_B_DQ7
Probe [2 07.69 39.2] On Device: s3.99
Wire Color: Blue Gauge: 28
Path (2 04.00 67.0) <5.5> [2 07.69 39.2]
Use ASRU Detector Shorts Test

Path (2 14.00 75.0) <7.5> [2 07.69 39.2]
Use S Bus Sense

What i need to do is , i have to read my first text file get the value in [ ] then compare that value with second text file . If value exist in second text file, i must search for Node name in that particular group. then i must copy that value and past in my text file 1
For example

Text file 1
[2 07.69 39.2] X7.786 Y3.489 215.574, 214.075, 204.067
Short +202.059, 204.040, 204.539, 205.539, 205.540, 207.039, 208.039, 207.539,
i read the value [] then match with second file so i will get this group from second file

Text file 2

-----------------------------------------------------------------------------
Node Name MEM_B_DQ7
Probe [2 07.69 39.2] On Device: s3.99
Wire Color: Blue Gauge: 28
Path (2 04.00 67.0) <5.5> [2 07.69 39.2]
Use ASRU Detector Shorts Test

Path (2 14.00 75.0) <7.5> [2 07.69 39.2]
Use S Bus Sense

From here i have to read the Node name which is MEM_B_DQ7


From this file i have to copy the Node name which is MEM_B_DQ7 and past it in my Text file 1
[2 07.69 39.2] X7.786 Y3.489 215.574, 214.075, 204.067
Short +202.059, 204.040, 204.539, 205.539, 205.540, 207.039, 208.039, 207.539,
MEM_B_DQ7
is that possible to do

thank you

I try to match the value in [] from text file and two
 Dim FileContents() As String = IO.File.ReadAllLines(TextBox1.Text)
		Dim FileContents1() As String = IO.File.ReadAllLines(TextBox2.Text)
		Dim fc As StreamWriter = File.CreateText("C:\kaya5.txt")
			 For Y As Integer = 0 To FileContents.GetUpperBound(0)
			For x As Integer = 0 To FileContents1.GetUpperBound(0)
				If Not FileContents(Y).Trim = "" Then
					Dim firstChar As Boolean

					If firstChar = FileContents(Y).IndexOf("[") Then
												Dim BeginIndex As Integer
						Dim EndIndex As Integer
						BeginIndex = FileContents(Y).IndexOf("[")
						EndIndex = FileContents(Y).IndexOf("]") + 1
						Dim Tx As String = FileContents(Y).Substring(BeginIndex, EndIndex - BeginIndex)

						Dim BeginIndex1 As Integer
						Dim EndIndex1 As Integer
						If FileContents1(x).Contains("[") Then
							BeginIndex1 = FileContents1(x).IndexOf("[")
							EndIndex1 = FileContents1(x).IndexOf("]") + 1
							Dim Tx1 As String = FileContents1(x).Substring(BeginIndex, EndIndex - BeginIndex)

							If Tx = Tx1 Then
								fc.WriteLine(FileContents1(x))
							End If

						End If
					End If
				End If


			Next
		Next
		fc.Close()


But can not do?

Is This A Good Question/Topic? 0
  • +

Replies To: Reading,Comparing and writing to text file

#2 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Reading,Comparing and writing to text file

Posted 18 August 2009 - 07:36 AM

Please use better variable names (still) FileContents and FileContents1 are very easy to visibly confuse - label them with their purpose - e.g. ProblemConnectionsFile and AllConnectionsFile or whatever makes sense to you.

Second it looks like the Node name is the line just before the matching line - so output FileContents1(x-1) to get that line???
Was This Post Helpful? 0
  • +
  • -

#3 kayatri  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 216
  • Joined: 05-May 09

Re: Reading,Comparing and writing to text file

Posted 18 August 2009 - 07:47 PM

View Postmark.bottomley, on 18 Aug, 2009 - 06:36 AM, said:

Please use better variable names (still) FileContents and FileContents1 are very easy to visibly confuse - label them with their purpose - e.g. ProblemConnectionsFile and AllConnectionsFile or whatever makes sense to you.

Second it looks like the Node name is the line just before the matching line - so output FileContents1(x-1) to get that line???



I came up with this code to solve my problem

Structure File2_ds
	Dim ds_node As String
	Dim ds_Value As String
End Structure
Dim dsList2 As List(Of File2_ds) = New List(Of File2_ds)
Sub ProcessFile2()
		'This routine will process the text file into a simple string data structure.
		Dim FileContents() As String = IO.File.ReadAllLines(TextBox2.Text)

		Dim NodeName As String = ""

		For I As Integer = 0 To FileContents.Length - 1
			Dim S As String = Trim(FileContents(I))
			If S <> "" Then
				If S.StartsWith("Node Name") Then
					NodeName = Trim(S.Substring(9))
				Else
					Dim BeginIndex As Integer = S.IndexOf("[")
					If BeginIndex <> -1 Then
						If NodeName <> "" Then
							Dim EndIndex As Integer = S.IndexOf("]")
							Dim Tx As String = S.Substring(BeginIndex, EndIndex - BeginIndex + 1)
							Dim ThisNode As File2_ds
							ThisNode.ds_node = NodeName
							ThisNode.ds_Value = Tx
							dsList2.Add(ThisNode)
							NodeName = ""
						End If
					End If
				End If
			End If
		Next
	End Sub
Sub ProcessFile1()
		'This routine will process file 1 and compare each line against the ds2 data structure to find a matching line 
		Dim FileContents() As String = IO.File.ReadAllLines(TextBox1.Text)
		Using sf As New IO.StreamWriter(TextBox1.Text)

			For I As Integer = 0 To FileContents.Length - 1
				Dim S As String = Trim(FileContents(I))
				If S <> "" Then
					If S.StartsWith("[") Then
						Dim EndIndex As Integer = S.IndexOf("]")
						Dim Tx As String = S.Substring(0, EndIndex + 1)

						Dim M2 As Integer = MatchToFile2(Tx)
						If M2 <> -1 Then
							FileContents(I) = FileContents(I).Insert(Tx, dsList2(M2).ds_Value)
							sf.WriteLine(FileContents(I))
													  Else
															
							   sf.WriteLine(FileContents(I))
							End If
						Else
									 sf.WriteLine(FileContents(I))
						End If
					Else
						  sf.WriteLine(FileContents(I))
					End If
							Next I
		End Using
	End Sub
Function MatchToFile2(ByVal V As String) As Integer
		For I As Integer = 0 To dsList2.Count - 1
			If V = dsList2(I).ds_Value Then
				Return I
				Exit For
			End If
		Next
		Return -1
	End Function
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
		ProcessFile1()
	End Sub


But it not writing anything as output.. is that anything wrong with this code?
Was This Post Helpful? 0
  • +
  • -

#4 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Reading,Comparing and writing to text file

Posted 18 August 2009 - 08:07 PM

Shouldn't this line:
FileContents(I) = FileContents(I).Insert(Tx, dsList2(M2).ds_Value)


be ds_Node not ds_Value - or else the search should look for ds_Node - not sure which, but you test ds_Value in Match2File and then replace it in the above statement with exactly what you found! - hence no change to the file.

A quick walk-through the code in the debugger would likely make it clear what is happening - use breakpoints to skip over the uninteresting stuff.

p.s. Is this a summer project for a coop job, or a full time job for your employer?

This post has been edited by mark.bottomley: 18 August 2009 - 08:11 PM

Was This Post Helpful? 0
  • +
  • -

#5 kayatri  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 216
  • Joined: 05-May 09

Re: Reading,Comparing and writing to text file

Posted 18 August 2009 - 08:13 PM

View Postmark.bottomley, on 18 Aug, 2009 - 07:07 PM, said:

Shouldn't this line:
FileContents(I) = FileContents(I).Insert(Tx, dsList2(M2).ds_Value)


be ds_Node not ds_Value - or else the search should look for ds_Node - not sure which, but you test ds_Value in Match2File and then replace it in the above statement with exactly what you found! - hence no change to the file.

A quick walk-through the code in the debugger would likely make it clear what is happening - use breakpoints to skip over the uninteresting stuff.


i change the ds_value to ds_node but nothing is write to the ouput file?

I feel everything is correct already.. I dont know what is the wrong
Was This Post Helpful? 0
  • +
  • -

#6 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Reading,Comparing and writing to text file

Posted 18 August 2009 - 08:25 PM

A couple of things:

1 - ProcessFile2 is not called from any of the included code - are you sure it runs.

2 - Using Debug.Print("Say something useful") to get a trace of execution dumped to the immediate window during execution - you can include parameters to print variable values as well

3- Code cleanup - you ALWAYS output the FileContents(I) at the end on all paths.
			For I As Integer = 0 To FileContents.Length - 1
				Dim S As String = Trim(FileContents(I))
				If S <> "" Then
					If S.StartsWith("[") Then
						Dim EndIndex As Integer = S.IndexOf("]")
						Dim Tx As String = S.Substring(0, EndIndex + 1)

						Dim M2 As Integer = MatchToFile2(Tx)
						If M2 <> -1 Then
							FileContents(I) = FileContents(I).Insert(Tx, dsList2(M2).ds_Value)
						End If
					End If
				End If
				sf.WriteLine(FileContents(I))
			Next I



4 - I think you are not using .Insert() correctly - I believe that the parameters are the offset of the string to insert and the string to insert. - You are feeding it 2 strings - maybe you want .Replace().

Bedtime here now... goodnight

This post has been edited by mark.bottomley: 18 August 2009 - 08:27 PM

Was This Post Helpful? 0
  • +
  • -

#7 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Reading,Comparing and writing to text file

Posted 19 August 2009 - 07:47 AM

I assume you have fixed this??? I don;t see any code in the included bit that actually writes out the FileContents() string array back to any file.
Was This Post Helpful? 0
  • +
  • -

#8 kayatri  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 216
  • Joined: 05-May 09

Re: Reading,Comparing and writing to text file

Posted 19 August 2009 - 06:33 PM

View Postmark.bottomley, on 19 Aug, 2009 - 06:47 AM, said:

I assume you have fixed this??? I don;t see any code in the included bit that actually writes out the FileContents() string array back to any file.



Thank you.. I fixed the problem ready. I never cal the sub of PrecessFile2.. Thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1