복사

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 

내가 "="오년 "로 선"대출 기간을 "복사 찾고 있어요를 대출 기간 "="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 

나는 비주얼 베이직 닷넷과 함께 이것에 대해 놀겠다는 거, 지금까지 봤는데, 이것은 내가

을 마련했습니다 것입니다 :

내가 달성하기 위해 희망의 궁극적 인 최종 결과는 이것이다

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

하나 개 그것을하는 방법 : 일단 파일을 통해

루프와 일부 배열로 오년 각각 이름을 넣어. 그런 다음 File.AppendText (path)를 사용하여 StreamWriter를 반환합니다. 이름 배열을 반복하여 StreamWriter에 기록한 다음 플러시 및 닫기합니다.

예제 코드는 입니다.