使用回车行终止符读/写固定长度的文本记录



实现此伪代码而不变为"非托管"的 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 行终止符在偏移量处读取/写入文本/平面文件的示例都没有。

你真的从未见过使用 StreamReader 或 File.ReadAllLines() 的代码吗?

    string path = @"d:temptestFile.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类。
在此示例中,我尝试将文件指针放置在文件的第四条记录上。

// The 5+5+2 is the assumed lenght of a line
const int recLength = 12;
string path = @"d:tempDATA1.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());
    }
}

最新更新