2012-04-19 9 views
4

温度データが入っているファイルがあります。私はそれから温度を抽出し、新しいファイルの温度だけで結果を保存する必要があります。私は、サブ文字列を使用してみましたが、それはときに使用できるすべて=すなわち34.23,12.3,54.01などファイルから特定のデータを抽出するにはどうすればよいですか?

BCAST:000D6F00017620E9, 02=34.23 
BCAST:000D6F00017620E9, 02=12.3 
BCAST:000D6F00017620E9, 02=54.01 
BCAST:000D6F00017620E9, 02=12.34 
BCAST:000D6F00017620E9, 02=16.22 

データを抽出する必要があります。

このファイルの内容がされ私は文字列としてファイルを読んで、それはちょうど最初の行の部分文字列を作る、残りは同じです。フォローは私のコードです。提案してください!このコードの

string temp2 = System.IO.File.ReadAllText(@"C:********\temperature.txt"); 
int c = temp2.IndexOf("="); 
string temp3 = temp2.Substring(c + 1); 
System.IO.File.WriteAllText(@"C:\*******\temperature2.txt",temp3); 

出力は次のとおりです。

34.23 
BCAST:000D6F00017620E9, 02=12 
BCAST:000D6F00017620E9, 02=54 
BCAST:000D6F00017620E9, 02=12 
BCAST:000D6F00017620E9, 02=16 
+1

ヒント: – Jason

答えて

6

ReadAllTextは、1つの文字列としてファイル全体を返します。配列の配列をループし、各行にサブ文字コードを使用する方が意味があります。

EDIT ReadAllLines静的呼び出しです:

string[] lines = System.IO.File.ReadAllLines(fileName); 

あるいは、ストリームに1行ずつ読み:

var sr = new StreamReader(fileStream); 
while (!sr.EndOfStream) 
{ 
    var line = sr.ReadLine(); 
    // .. per line sub string 
} 

EDIT 2を私は(完全なソリューションを働きました私は、すべてのスタイルを読むのではなく、ストリームの読み込みを好む - 非常に大きなファイルの方が効率的です - それで慣れるのは良い習慣です)

var sb = new StringBuilder(); 

using (var file = new FileStream("C:/tmp/temps.txt", FileMode.Open, FileAccess.Read)) 
{ 
    var sr = new StreamReader(file); 

    while (!sr.EndOfStream) 
    { 
    var nextLine = sr.ReadLine(); 
    int indexOfEqualSign = nextLine.IndexOf("="); 

    if (indexOfEqualSign == -1 || indexOfEqualSign == nextLine.Length) 
     continue; 

    string rightHandSide = nextLine.Substring(indexOfEqualSign + 1); 
    sb.AppendLine(rightHandSide); 
    } 
} 

File.WriteAllText("C:/tmp/temps2.txt", sb.ToString()); 
+0

サンキューファイルの各行を処理するために、ループのいくつかの並べ替えを使用します。私はそれを試してみます – user1344602

+0

それは素晴らしい仕事をした。ありがとう、ストリームリーダー確かに良い動作します。 – user1344602

1

あなたは正しい軌道に乗っているが、あなたは、ファイル内の行をループする必要があり、その後、=に分割を行います。

string[] lines = File.ReadAllLines(@"C:\********\temperature.txt"); 
StringBuilder temperatures = new StringBuilder(); 

foreach (string line in lines) 
{ 
    string[] parts = line.Split('='); 

    if (lines.Length > 1) 
    { 
     tempatures.Append(lines[1])); 
     tempatures.Append("\n"); 
    } 
} 

File.WriteAllText(@"C:\*******\temperature2.txt", tempatures.ToString()); 
+0

あなたのお手伝いをしてくれてありがとう – user1344602

1

は、一度にそれぞれの行を読むには、上の分割しました等号。含めてください :System.IO StreamReaderをのため

try 
{ 
    // Create an instance of StreamReader to read from a file. 
    // The using statement also closes the StreamReader. 
    using (StreamReader sr = new StreamReader(@"C:********\temperature.txt")) 
    { 
     String line; 
     // Read and display lines from the file until the end of 
     // the file is reached. 
     while ((line = sr.ReadLine()) != null) 
     { 
      try 
      { 
       Console.WriteLine(line.Split('=')[1]); 
      } 
      catch 
      { 
       Console.WriteLine("Contained No equals sign: " + line); 
      } 
     } 
    } 
} 
catch (Exception e) 
{ 
    // Let the user know what went wrong. 
    Console.WriteLine("The file could not be read:"); 
    Console.WriteLine(e.Message); 
} 

出典:http://msdn.microsoft.com/en-us/library/db5x7c0d.aspx

+0

助けてくれてありがとう – user1344602

0
string readFile = @"C:\****\temperature.txt"); 
string writeFile = @"C:\****\temperature2.txt"); 
List<string> lines = new List<string>lines; 
.... 
string sr = new StreamReader(readFile); 
foreach (string line in sr.ReadAllLines()) 
{ 
    int c = line.IndexOf("="); 
    if (c >= 0) { 
     lines.Add(line.SubString(c+1); 
    } 
} 

if (lines.Count > 0) { 
    System.IO.File.WriteAllText(writeFile, 
    string.Join(Environment.NewLine, lines.ToArray*()); 
} 
+0

あなたのお手伝いをしてくれてありがとう – user1344602

関連する問題