2016-12-09 5 views
-1

テキストファイルを読み込んでいるときに特定の行に自動的に番号を追加する必要がある 5行を保存するとします) 私は私のプログラムが自動的にテキストボックスに質問の行に乗っ質問#「1-2-3-4-5毎回を追加する必要が だから、その特定の行のテキストファイルを読み込むときにインクリメントする番号を自動的に追加する

質問#1 <ようになります。 - - 必要なもの

回答:a

回答:b

件の

回答:

答C:D

質問#2 < -----私はここに

+1

...と老後の控え:*あなたは何を試しましたか?*私たちはここで宿題をするつもりはありません。 [Ask]を読んで[Tour] <------必要なものを取ってください – Plutonix

+1

"Question"という単語で始まる行に達すると、カウンター変数が増加します。 – LarsTech

+0

問題ループとその要素をどのように通過させるかがわかりません。私はそれを理解するための例が必要だろう –

答えて

0

を必要とするものは、あなたが後にあるものの基本です。 この最初の例では、この第二の例は、すべての質問のために、4つの答えが常に存在することを前提に行く「質問」

Imports System.IO 
    Sub Main() 

     Dim sourceFile As String 
     Dim newFile As String 
     Dim tracker As Integer = 0 


     sourceFile = "C:\\temp\\43494_1.txt" 
     newFile = "C:\\temp\\43494_1_new.txt" 


     Using sw As New StreamWriter(newFile) 'write the new file 
      Using sr As New StreamReader(sourceFile) 'read the source file 
       Dim currentLine As String = String.Empty 
       While (sr.Peek >= 0) 'while there is something to read 
        currentLine = sr.ReadLine() 
        If currentLine.ToLower.StartsWith("question") Then 
         tracker = tracker + 1 'increment the question tracker 
         sw.WriteLine(currentLine & tracker) 
        Else 
         sw.WriteLine(currentLine) 
        End If 

       End While 
      End Using 
     End Using 

    End Sub 

あなたは言葉で始まる各行に質問番号を追加します前提に行きますつまり、各質問は実際には5行のブロックであり、スニペットはソースファイルの1行目が最初の質問であるとみなします。

Imports System.IO 
    Sub Main() 
     Dim sourceFile As String 
     Dim newFile As String 
     sourceFile = "C:\\temp\\43494_1.txt" 
     newFile = "C:\\temp\\43494_1_new.txt" 

     Dim lineTracker As Integer = 0 
     Dim questionTracker As Integer = 0 

     Using sw As New StreamWriter(newFile) 'write the new file 
      Using sr As New StreamReader(sourceFile) 'read the source file 
       Dim currentLine As String = String.Empty 
       While (sr.Peek >= 0) 'while there is something to read 
        currentLine = sr.ReadLine() 

        'reset the lineTracker if you need to 
        If lineTracker = 5 Then 
         lineTracker = 0 
        End If 

        lineTracker = lineTracker + 1 'increment the lineTracker 

        If lineTracker = 1 Then 
         questionTracker = questionTracker + 1 'increment the question tracker 
         sw.WriteLine(currentLine & questionTracker) 
        Else 
         sw.WriteLine(currentLine) 
        End If 
       End While 
      End Using 
     End Using 
    End Sub 
+0

ありがとう!そのトリックをした。私はそれを自分で見つけられませんでした。私はこれで非常に新しいです。 –

関連する問題