实现此伪代码而不会变为“非托管”的 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 行终止符。