2011-06-28 7 views
3

私は、テキストファイルから情報を読み取り、それらを分類してデータベースに格納するアプリケーションを持っています。 1つのカテゴリについては、現在の行の直後にある行をチェックして特定のキーワードを探す必要がありますか?ファイルの次の行を一度だけ読み取る

どうすればこの行を読むことができますか?ストリームリーダーが現在開いている回線を既に持っている場合に発生します。

私はVS2010でc#を使用しています。

編集

以下のコードのすべてが中(!sReader.EndOfStream)ループ

string line = sReader.ReadLine(); //Note: this is used way above and lots of things are done before we come to this loop 

for (int i = 0; i < filter_length; i++) 
{ 
     if (searchpattern_queries[i].IsMatch(line) == true) 
     { 
       logmessagtype = selected_queries[i]; 

       //*Here i need to add a if condition to check if the type is "RESTARTS" and i need to get the next line to do more classification. I need to get that line only to classify the current one. So, I'd want it to be open independently * 

       hit = 1; 
       if (logmessagtype == "AL-UNDEF") 
       { 
        string alid = AlarmID_Search(line); 
        string query = "SELECT Severity from Alarms WHERE ALID like '" +alid +"'"; 
        OleDbCommand cmdo = new OleDbCommand(query, conn); 
        OleDbDataReader reader; 
        reader = cmdo.ExecuteReader(); 
        while (reader.Read()) 
        { 
         if (reader.GetString(0).ToString() == null) 
         { } 
         else 
         { 
          string severity = reader.GetString(0).ToString(); 
          if (severity == "1") 
           //Keeps going on..... 

である。また、開かれた.logファイルは行くかもしれません50メガバイトまで!...!だから私は本当にすべての行を読んでトラックを保つことを好まないのです!

+0

StreamReaderには「行がありません」がありません。 –

+0

'StreamReader.ReadLine()'を呼び出すことはできませんか? – rossipedia

+0

@ブライアン:ちょうどその行に移動するだろうか?私は次の繰り返しのためにその行をもう一度読む必要があります。 – techmanc

答えて

4

は単に

string[] lines = File.ReadAllLines(filename); 

を使用してfor (int i = 0; i < lines.Length; i ++)ループを持つファイルを処理します。

大きなファイルの場合は、「前の行」をキャッシュするか、帯域外のReadLine()を実行するだけです。

+0

あなたはどのようにアウトバンドのreadlineを行うのですか? – techmanc

1

reader.ReadLine()をもう一度呼び出すことはできませんか?または、ループの次の反復でその行を使用する必要があるという問題がありますか?

ファイルがかなり小さい場合は、File.ReadAllLines()を使用してファイル全体を読み取ると考えましたか?明らかに他の方法ではそれほどクリーンではありませんが、大きなファイルに対してはもっとメモリが必要です。

EDITは:ここでは、代替として、いくつかのコードです:

using (TextReader reader = File.OpenText(filename)) 
{ 
    string line = null; // Need to read to start with 

    while (true) 
    { 
     if (line == null) 
     { 
      line = reader.ReadLine(); 
      // Check for end of file... 
      if (line == null) 
      { 
       break; 
      } 
     } 
     if (line.Contains("Magic category")) 
     { 
      string lastLine = line; 
      line = reader.ReadLine(); // Won't read again next iteration 
     } 
     else 
     { 
      // Process line as normal... 
      line = null; // Need to read again next time 
     } 
    } 
} 
+0

私は次の繰り返しのための線が必要です。私はちょうど現在の行を分類するための参照としてそれを必要とする..私は現在のreadlineから隔離された行を開くことができる方法はありますか...私はすべての行を読むことについて考えたが、ファイルが巨大な場合があります。 ..それは理想的ではありませんでした! – techmanc

+0

@techmanc: "巨大な"大きさは正確ですか?あなたは常にあなたが読んだ行のコピーを保持することができました。トリッキーなビットは、次の行を読む代わりにその行を使用していることを確認しています。 –

+0

私は100のファイルを読んで、それを1回の反復のためにDBに入れました。そして、いくつかのファイルは、おそらく50-100 MBです! – techmanc

0

ストリームの位置を保存してから、ReadLineを呼び出して、その位置にシークバックすることができます。しかし、これはかなり非効率的です。

ReadLineの結果を「バッファ」に格納し、可能であればそのバッファをソースとして使用します。空の場合は、ReadLineを使用します。

0

私は本当に、ファイルIOの専門家ではないよ...しかし、なぜこのような何かではない:あなたが読んで行が2つの変数を宣言し始める前に

を。

public void ProcessFile(string filename) 
{ 
    string line = null; 
    string nextLine = null; 
    using (StreamReader reader = new StreamReader(filename)) 
    { 
     line = reader.ReadLine(); 
     nextLine = reader.ReadLine(); 
     while (line != null) 
     { 
      // Process line (possibly using nextLine). 

      line = nextLine; 
      nextLine = reader.ReadLine(); 
     } 
    } 
} 

は、これは基本的には次のとおりです。次に

string currentLine = string.Empty 
string previousLine = string.Empty 

あなたが読んでいる間、...ここで

previousLine = currentLine; 
currentLine = reader.ReadLine(); 
5

は、現在の行がすでに利用できる次の行を持ちながらを処理するためのイディオムです「キュー」、または「1行先読み」の項目が最大2つあります。

編集:簡体字です。

関連する問題