2009-04-26 5 views
1

私はそうのように、タブ区切りのテキストファイルがあります。コピー

name \t loan period \t loan amount 
John \t 5 years  \t 6000 
Sarah \t 5 years  \t 6000 
Jane \t 1 month  \t 100 

をここで、「貸出期間」=「5年」に "私は行をコピーしていますよ比較を表示するために、「ローン期間」=「1ヶ月」を選択します。新しい行は、結果のファイルの末尾に追加されます。

私が達成したいと考えて、究極の最終結果がこれです:

name \t loan period \t loan amount 
John \t 5 years  \t 6000 
Sarah \t 5 years  \t 6000 
Jane \t 1 month  \t 100 
John \t 1 month  \t 100 
Sarah \t 1 month  \t 100 

私は、Visual Basic .NETでこれについていじるしてきた、これまでのところ、これは私が

を作ってみたものです
Dim strData As String 
    Dim i As Short 
    Dim strLine() As String 
    lngSize = 0 

FileOpen(1, txtloanlistinput.Text, OpenMode.Input) 
    While Not EOF(1) 
     i = i + 1 
     strData = LineInput(1) 
    End While 
    FileClose(1) 
    ReDim loanlist(i) 
    strData = "" 
    lngSize = i 
    i = 0 
    FileOpen(2, txtloanlistinput.Text, OpenMode.Input) 
    While Not EOF(2) 
     i = i + 1 
     strData = LineInput(2) 
     If i = 1 Then 
      strData = LineInput(2) 
     End If 
     strLine = Split(strData, Chr(9)) 
     loanlist(i).strName = strLine(0) 
     loanlist(i).strLoanPeriod = strLine(1) 
     loanlist(i).curLoanAmount = strLine(2) 
    End While 
    FileClose(1) 
    FileClose(2) 

私は進行方法については空白を描いており、助けを求めると思っていました。それをやっての

答えて

1

醜いですが、始まりです。

// C#

 OpenFileDialog dialog = new OpenFileDialog();    
     dialog.ShowDialog();    
     string filePath = dialog.FileName; 
     //in this list we store the match of 5 years. 
     List<string[]> fiveYears = new List<string[]>(); 
     //open a reader. 
     StreamReader tr = new StreamReader(filePath); 
     //reaing 1° line. 
     string line=tr.ReadLine(); 
     while (line != null && line != "" && line != "\n") 
     { 
      //split de line by tabs. 
      string[] lineByTabs = line.Split('\t'); 
      //if the second term equals '5 years' 
      if (lineByTabs[1].Equals("5 years")) 
      { 
       //change from 5 years to 1 month, and to a lonan of 100. 
       lineByTabs[1] = "1 month"; 
       lineByTabs[2] = "100"; 
       fiveYears.Add(lineByTabs);     
      } 
      line = tr.ReadLine(); 
     } 
     //close reader 
     tr.Close(); 
     //open the file and apend lines. 
     TextWriter tw = new StreamWriter(filePath, true); 

     foreach (string[] lines in fiveYears) 
     { 
      tw.WriteLine(lines[0] + "\t" + lines[1] + "\t" + lines[2]); 
     } 
     tw.Close(); 

    } 

「vb.net

Dim dialog As OpenFileDialog = New OpenFileDialog 
    Dim filePath, line As String 
    Dim fiveYears As List(Of String()) = New List(Of String()) 
    Dim lineByTabs As String() 
    Dim tr As StreamReader 
    Dim tw As TextWriter 
    dialog.ShowDialog() 
    filePath = dialog.FileName 

    tr = New StreamReader(filePath) 

    line = tr.ReadLine() 

    While Not line = "" 

     lineByTabs = line.Split(vbTab) 

     If lineByTabs(1).Equals("5 years") Then 

      lineByTabs(1) = "1 month" 
      lineByTabs(2) = "100" 
      fiveYears.Add(lineByTabs) 
     End If 
     line = tr.ReadLine() 
    End While 

    tr.Close() 

    tw = New StreamWriter(filePath, True) 

    For Each lines As String() In fiveYears 
     tw.WriteLine(lines(0) + vbTab + lines(1) + vbTab + lines(2)) 
    Next 
    tw.Close() 

はそれが

ノート役に立てば幸い: flieは改行で終了する必要があります。与えられたフォーマット(3列)をフォローします。

+0

ありがとう、ご返信ありがとうございます。 –

+0

助けてくれると心配しないでください – vaquito

1

一つの方法:

ループファイルを使用して一度、いくつかの配列に5年間でそれぞれの名前を入れました。 その後、File.AppendText(path)を使用してStreamWriterを返します。 名前の配列をループし、StreamWriterに書き込み、次にフラッシュして閉じる。

のコード例を参照してください。