-2

「アンマネージ」になることなく、この疑似コードを実装する C# の例:

dataRec = dataRec.Key [5] + dataRec.Ptr [5] + CrLf [2];
recSize = sizeof (dataRec); // recSize = 12
aCrLf = CarriageReturn (ASCII 13) + LineFeed (ASCII 10); // define CrLf constant
fs = Open (textFile);
dataRec = "A    " + "00001" + aCrLf; // initialize 1st Row
Write (fs, dataRec, recSize * (1 - 1), recsize); // write 1st row at offset 0
dataRec = "AB   " + "00002" + aCrLf; // initialize 2nd Row
Write (fs, dataRec, recSize * (2 - 1), recsize); // write 2nd row at offset 12
dataRec = "ABC  " + "00003" + aCrLf; // initialize 3rd Row
Write (fs, dataRec, recSize * (3 - 1), recsize); // write 3rd row at offset 24
//
Read (fs, dataRec, recSize * (1 - 1), recsize); // Read 1st row at offset 0
sWork = dataRec;    // convert to string.
Console.WriteLine(sWork);   // show 1st row
Read (fs, dataRec, recSize * (2 - 1), recsize); // Read 2nd row at offset 12
sWork = dataRec;    // convert to string.
Console.WriteLine(sWork);   // show 2nd row
Read (fs, dataRec, recSize * (3 - 1), recsize); // Read 3rd row at offset 24
sWork = dataRec;    // convert to string.
Console.WriteLine(sWork);   // show 3rd row
Close (fs);

DBL、C、または VBA を使用すると、オフセットでの固定長テキスト行の読み取り/書き込み、つまりランダム アクセスが非常に簡単になります。しかし、私が見たバイナリ読み取り/書き込みの C# の例では、「アンマネージ コード」が使用されており、オフセットで CrLf 行ターミネータを使用して読み取り/書き込みテキスト/フラット ファイルを確認した例はありません。

4

1 に答える 1

0

StreamReader または File.ReadAllLines() を使用するコードを見たことがありませんか?

    string path = @"d:\temp\testFile.txt";

    if (File.Exists(path))
    {
         string[] loadedLines = File.ReadAllLines(path);
         if(loadedLines.Length >= 5)
         {
              string line5 = loadedLines[4];
              if(line5.Length >= 10)
              {
                  string key = line5.Substring(0,5);
                  string value = line5.Substring(5,5);
                  .....
              }
         }
    }

書き込み部分はこれかもしれません

     List<string> lines = loadedLines.ToList();
     lines.Add("00009VAL09");
     lines[4] = "00008XXXXX";
     File.WriteAllLines(path, lines);

もちろん、これらのメソッド (ReadAllLines/WriteAllLines) は、読み書きするファイルが非常に大きい場合に最適なオプションではありませんが、NET でテキスト ファイルを操作するのは非常に簡単です。

MSDN
File.IO クラスから
一般的な IO タスク

テキスト ファイルでランダム アクセスを使用することは少し一般的ではありませんが、たとえば BinaryReader および BinaryWriter クラスを使用することは可能です。
この例では、ファイル ポインターをファイルの 4 番目のレコードに配置しようとしています。

// The 5+5+2 is the assumed lenght of a line
const int recLength = 12;

string path = @"d:\temp\DATA1.txt";

if (File.Exists(path))
{
    int recNum = 4;

    string key;
    string value;
    using(BinaryReader br = new BinaryReader(File.Open(path, FileMode.Open)))
    {
        // The key point is the Position property that should be set using
        // some kind of simple math to the exact position needed
        br.BaseStream.Position = recNum * recLength;

        // Read the 5 bytes and build the key and value string, 
        // note that reading (or writing) advances the Position 
        key = new string(br.ReadChars(5));
        value = new string(br.ReadChars(5));
    }
    Console.WriteLine(key)        ;
    Console.WriteLine(value)        ;

    key = "00009";
    value = "KKKKK";

    using(BinaryWriter bw = new BinaryWriter(File.Open(path, FileMode.Open)))
    {
        // Again the math required to position for the write
        bw.BaseStream.Position = recNum * recLength;
        bw.Write(key.ToCharArray());
        bw.Write(value.ToCharArray());
    }

}
于 2013-09-14T19:45:28.313 に答える