首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

VC++ CFile::modeCreate、modeWrite 數值含義說明

     編輯:程式設計綜合問答

現象

單步偵錯時觀察變數,發現 CFile::modeCreate = 4096、CFile::modeWrite = 1,以為是錯誤碼。

說明

這些是正常的旗標常數值,非錯誤。**

  • modeRead = 0x0000 (0)
  • modeWrite = 0x0001 (1)
  • modeReadWrite = 0x0002 (2)
  • modeCreate = 0x1000 (4096)
  • modeNoTruncate = 0x2000 (8192)
  • typeText = 0x4000 (16384)
  • typeBinary = 0x8000 (32768)
  • shareDenyNone = 0x0040 (64)
  • shareDenyRead = 0x0030 (48)
  • shareDenyWrite = 0x0020 (32)
  • shareExclusive = 0x0010 (16)
  • shareCompat = 0x0000 (0)

用法範例

CFile file;
// 建立新檔案、寫入、二進位、獨佔
if (file.Open(_T("data.dat"), 
    CFile::modeCreate | CFile::modeWrite | CFile::typeBinary | CFile::shareExclusive)) {
    file.Write(buffer, size);
    file.Close();
}

位元組合原理

旗標設計為 **位元遮罩**,以 | (bitwise OR) 組合:

modeCreate (0x1000) | modeWrite (0x0001) = 0x1001 (4097)

偵錯時看到 4096、1 等數值,代表該旗標被設定,完全正常。

常見組合

需求旗標組合
讀取現有檔案 (不存在失敗)modeRead | shareDenyNone
建立/覆寫檔案寫入modeCreate | modeWrite | typeBinary
開啟現有檔案追加 (不截斷)modeWrite | modeNoTruncate
讀寫現有檔案modeReadWrite

理解旗標位元組合設計,即可正確控制檔案開啟模式與共享行為。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved