2009-10-12 106 views
4

我正在嘗試最基本的東西....用C++編寫文件,但文件沒有被寫入。我也沒有得到任何錯誤。也許我錯過了顯而易見的東西......或什麼?無法在C++中編寫文件

我認爲我的代碼有問題,但我也嘗試了一個我在網上找到的示例,但仍然沒有創建文件。

這是代碼:

ofstream myfile; 
myfile.open ("C:\\Users\\Thorgeir\\Documents\\test.txt"); 
myfile << "Writing this to a file.\n"; 
myfile.close(); 

我也試着創建文件手動事前,但它沒有更新的。

我正在運行Windows 7 64位,如果這有什麼關係。這就像文件寫入操作是完全禁止的,並且沒有顯示錯誤消息或例外。

+0

我知道這不是一個解決方案,但這發生在我之前。只需用** fstream **替換** ofstream **即可。 – AraK 2009-10-12 22:22:26

+0

你確定有一個名爲「Documents」的文件夾嗎?如果我沒有弄錯,它通常被命名爲「我的文檔」... – 2009-10-12 23:06:21

+1

@Jerry自Vista以來,它變成了「文檔」。 – Corey 2009-10-12 23:23:54

回答

2

你需要寫模式來打開文件:

myfile.open ("C:\\Users\\Thorgeir\\Documents\\test.txt", ios::out); 

請務必看爲第二個參數的其他選項,以及。如果您正在編寫二進制數據,則需要例如ios::binary

你也應該打開它後檢查流:

myfile.open(... 
if (myfile.is_open()) 
    ... 

更新:

阿拉克是正確的,我忘了,一個ofstream在默認狀態下寫模式,所以這不是問題。

也許你根本沒有對目錄的寫/創建權限? Win7默認許多目錄具有「拒絕全部」的特殊權限。或者該文件可能已經存在並且是隻讀的?

+3

@Tim好的一點,但std :: ios :: out默認設置在** ofstream **的構造函數中。 – AraK 2009-10-12 22:28:31

1

您是否閱讀過Windows Vista和7中的UAC(用戶帳戶控制)和UAC虛擬化/數據重定向?您的文件實際上可能位於虛擬商店中。

User Account Control Data Redirection

你舉的例子輸出目錄中的用戶,所以我不認爲這會成爲問題,但它是值得一提的有什麼東西的可能性是非常令人沮喪,如果你不尋找出它!

希望這會有所幫助。通過扭轉那條斜線

if(! myfile) 
{ 
cerr << "You have failed to open the file\n"; 

//find the error code and look up what it means. 
} 
0

試試這個。
即使Windows瞭解反斜槓也是如此。

ofstream myfile("C:/Users/Thorgeir/Documents/test.txt"); 

如果有任何錯誤,你可以測試:

if (!myfile) 
{ 
    std::cout << "Somthing failed while opening the file\n"; 
} 
else 
{ 
    myfile << "Writing this to a file.\n"; 
    myfile.close(); 
} 
  • 確保目錄存在。
  • 如果文件存在確保它是可寫的(由你)
  • 檢查要寫入可寫的目錄(你)
+0

你如何「找到錯誤代碼」? – 2011-06-30 15:50:52

+0

@Tomalak:http://msdn.microsoft.com/en-us/library/ms679360(VS.85).aspx – 2011-06-30 23:09:31

+0

在您的答案中值得一提的是,這是Microsoft特有的。對於流失敗,C++沒有「錯誤代碼」;只有失敗模式。 – 2011-07-01 01:41:32

2

開始:

+1

爲什麼推薦使用不是目標平臺原生的路徑分隔符?特別是當它什麼都不解決?奇。 – 2011-06-30 15:49:49

+0

@Tomalak:不是。使用'\'最多是容易出錯的,因爲它是一個轉義字符(沒有編譯時檢查以確保它被正確使用)。 MS十年前就認識到這個問題,並增加了對'/'的支持,該計劃旨在與其他操作系統兼容。不幸的是,他們陷入了向後兼容性問題/ – 2011-06-30 16:39:02

+0

我沒有看到'\\'的問題。 – 2011-06-30 16:41:41

1

此代碼應該捕獲任何錯誤。如果遇到任何錯誤,很可能它是一個權限問題。確保你可以讀/寫你創建的文件的文件夾

#include "stdafx.h" 
#include <fstream> 
#include <iostream> 

bool CheckStreamErrorBits(const std::ofstream& ofile); 

int _tmain(int argc, _TCHAR* argv[]) { 
std::ofstream ofile("c:\\test.txt"); 
if(ofile.is_open()) { 
    CheckStreamErrorBits(ofile); 
    ofile << "this is a test" << std::endl; 
    if(CheckStreamErrorBits(ofile)) { 
    std::cout << "successfully wrote file" << std::endl; 
    } 
}else { 
    CheckStreamErrorBits(ofile); 
    std::cerr << "failed to open file" << std::endl; 
} 

ofile.close(); 
return 0; 
} 

//return true if stream is ok. return false if stream has error. 
bool CheckStreamErrorBits(const std::ofstream& ofile) { 
bool bError=false; 
if(ofile.bad()) { 
    std::cerr << "error in file stream, the bad bit is set" << std::endl; 
    bError=true; 
}else if(ofile.fail()) { 
    std::cerr << "error in file stream, the fail bit is set" << std::endl; 
    bError=true; 
}else if(ofile.eof()) { 
    std::cerr << "error in file stream, the eof bit is set" << std::endl; 
    bError=true; 
} 
return !bError; 
} 

更新: 我只是測試我在Windows 7恩特普賴斯的代碼,它第一次失敗(失敗位設置)。然後,我關閉用戶帳戶控制(UAC)並再次測試並寫入文件。這可能是你看到的同樣的問題。要關閉UAC,請轉到:

控制面板(查看小圖標)|用戶帳戶|更改用戶帳戶控制設置。將其設置爲永不通知,然後單擊確定按鈕。您必須重新啓動才能使更改生效。

我很好奇如何讓它與UAC一起工作,我會研究一下。

0

使用FileMon並從您的進程中查找失敗的WriteFile調用。