簡體   English   中英

CFile返回調試斷言

[英]CFile returns debug assertion

我在Visual C ++中有一個舊項目,我正嘗試將其遷移到Visual Studio2013。當我驗證是否存在txt文件時, CFile返回調試斷言錯誤。 代碼是:

if (!txt_file.Open(txt_name,    CFile::modeWrite | CFile::shareDenyWrite  | CFile::typeText))
{
    //action if the file exists
}

有什么問題,我做錯了什么? 謝謝

LE:

txt_file在類trace聲明為: CStdioFile txt_file
txt_name在類trace名為open_file的方法中聲明為: private CString txt_name
方法open_file包含返回調試斷言錯誤的if語句。

您可能正在使用:

CFile txt_file;

CFile不支持文本模式。

要以文本模式打開,您可以更改為:

CStdioFile txt_file;

那應該可以解決問題(至少,在這種情況下使用CFile生成一個斷言)。


如果您已經在使用CStdioFile ,則打開模式(的組合)可能存在問題。 作為測試,嘗試刪除CFile::shareDenyWrite 也可能有安全限制。

mfc \\ filecore.cpp行:179

最好是通過調試器逐步解決,或者查看filecore.cpp Line: 179 ,查看在那里檢查了什么(我會為您查找,但現在沒有Visual Studio 2013 -可能是開放模式)。

更新

這是第179行:

// shouldn't open an already open file (it will leak)

ASSERT(m_hFile == INVALID_HANDLE_VALUE);

該文件已經打開。 因此,無需再次打開它,或者首先需要關閉,才能以其他打開模式打開。

txt_file.Close();

或測試文件是否打開(對於CMemFile無效):

if (txt_file.m_hFile != CFile::hFileNull) { // file already open
    txt_file.Close();
}

解決了 !

我沒有使用問題中的代碼來檢查文件是否存在,而是使用了以下代碼:

  CFileStatus sts; //status flag
        bool chkifFileExists = CFile::GetStatus(txt_name, sts); // return TRUE if the file exists else return false




        if (!(chkifFileExists))

{
 //do something
}

謝謝大家的支持!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM