Advertisement
byattack

copying from a tab delimited text file to itself byattack

Feb 10th, 2012
604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. name t loan period t loan amount
  2. John t 5 years t 6000
  3. Sarah t 5 years t 6000
  4. Jane t 1 month t 100
  5.  
  6. name t loan period t loan amount
  7. John t 5 years t 6000
  8. Sarah t 5 years t 6000
  9. Jane t 1 month t 100
  10. John t 1 month t 100
  11. Sarah t 1 month t 100
  12.  
  13. Dim strData As String
  14. Dim i As Short
  15. Dim strLine() As String
  16. lngSize = 0
  17.  
  18. FileOpen(1, txtloanlistinput.Text, OpenMode.Input)
  19. While Not EOF(1)
  20. i = i + 1
  21. strData = LineInput(1)
  22. End While
  23. FileClose(1)
  24. ReDim loanlist(i)
  25. strData = ""
  26. lngSize = i
  27. i = 0
  28. FileOpen(2, txtloanlistinput.Text, OpenMode.Input)
  29. While Not EOF(2)
  30. i = i + 1
  31. strData = LineInput(2)
  32. If i = 1 Then
  33. strData = LineInput(2)
  34. End If
  35. strLine = Split(strData, Chr(9))
  36. loanlist(i).strName = strLine(0)
  37. loanlist(i).strLoanPeriod = strLine(1)
  38. loanlist(i).curLoanAmount = strLine(2)
  39. End While
  40. FileClose(1)
  41. FileClose(2)
  42.  
  43. OpenFileDialog dialog = new OpenFileDialog();
  44. dialog.ShowDialog();
  45. string filePath = dialog.FileName;
  46. //in this list we store the match of 5 years.
  47. List<string[]> fiveYears = new List<string[]>();
  48. //open a reader.
  49. StreamReader tr = new StreamReader(filePath);
  50. //reaing 1° line.
  51. string line=tr.ReadLine();
  52. while (line != null && line != "" && line != "n")
  53. {
  54. //split de line by tabs.
  55. string[] lineByTabs = line.Split('t');
  56. //if the second term equals '5 years'
  57. if (lineByTabs[1].Equals("5 years"))
  58. {
  59. //change from 5 years to 1 month, and to a lonan of 100.
  60. lineByTabs[1] = "1 month";
  61. lineByTabs[2] = "100";
  62. fiveYears.Add(lineByTabs);
  63. }
  64. line = tr.ReadLine();
  65. }
  66. //close reader
  67. tr.Close();
  68. //open the file and apend lines.
  69. TextWriter tw = new StreamWriter(filePath, true);
  70.  
  71. foreach (string[] lines in fiveYears)
  72. {
  73. tw.WriteLine(lines[0] + "t" + lines[1] + "t" + lines[2]);
  74. }
  75. tw.Close();
  76.  
  77. }
  78.  
  79. Dim dialog As OpenFileDialog = New OpenFileDialog
  80. Dim filePath, line As String
  81. Dim fiveYears As List(Of String()) = New List(Of String())
  82. Dim lineByTabs As String()
  83. Dim tr As StreamReader
  84. Dim tw As TextWriter
  85. dialog.ShowDialog()
  86. filePath = dialog.FileName
  87.  
  88. tr = New StreamReader(filePath)
  89.  
  90. line = tr.ReadLine()
  91.  
  92. While Not line = ""
  93.  
  94. lineByTabs = line.Split(vbTab)
  95.  
  96. If lineByTabs(1).Equals("5 years") Then
  97.  
  98. lineByTabs(1) = "1 month"
  99. lineByTabs(2) = "100"
  100. fiveYears.Add(lineByTabs)
  101. End If
  102. line = tr.ReadLine()
  103. End While
  104.  
  105. tr.Close()
  106.  
  107. tw = New StreamWriter(filePath, True)
  108.  
  109. For Each lines As String() In fiveYears
  110. tw.WriteLine(lines(0) + vbTab + lines(1) + vbTab + lines(2))
  111. Next
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement