-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 类
Common IO 任务

在文本文件上使用随机访问有点不常见,但是例如可以使用 BinaryReader 和 BinaryWriter 类。
在此示例中,我尝试将文件指针定位在文件的第四条记录上。

// 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 回答