C# 相当于 C++ ostream::tellp 用于软盘上的大小限制

C# equivalent of C++ ostream::tellp for size limit on diskette

本文关键字:用于 C++ 相当于 ostream tellp 软盘      更新时间:2023-10-16

C++ ostream::tellp的StreamWriter是否有C#等效项?我正在将一些旧的C++代码移植到 C#,但客户端仍然希望继续使用软盘(阅读:旧设备),所以我需要找到一种方法来查找文件指针位置或查找我已经写入磁盘的数量。

以下是我到目前为止创建的方法:

 private bool isDisketteBoundary(ref StreamWriter swOutput, int nCurrentDisketteNo) {
      // Get current file pointer position
      // long filePosition = nOStream.tellp(); <-- C++ code
      long filePosition = 0; // <-- needs to change to find file pointer position
      // Valid?
      if(filePosition != -1) {
           // Is the new size over a boundary?
           float numDiskettes = (float)((float)filePosition / (float)Constants.DisketteSize);
           int disketteCount = Convert.ToInt32(Math.Ceiling(numDiskettes));
           // Is the diskette count larger than the current index?
           return (nCurrentDisketteNo < disketteCount) ? true : false;
      }
      else {
           throw new Exception("Unable to get file pointer from StreamWriter");
      }
 }

我想你正在寻找

swOutput.BaseStream.Position

请参阅 MSDN: http://msdn.microsoft.com/en-us/library/system.io.stream.position.aspx 。