我正在嘗試以下代碼:
public static void main(){
var file = FileStream.open("string.txt", "rw");
assert (file != null);
try{
file.puts (new DateTime.now_local ().to_string ());
file.putc ('\n');
file.flush();
} catch(Error e){
print("Error occurred while writing.");
}
}
上面的代碼編譯正常,運行也沒有任何錯誤。但是,沒有對“string.txt”檔案進行任何更改。錯誤在哪里,如何糾正?
uj5u.com熱心網友回復:
問題在這里:
var file = FileStream.open("string.txt", "rw");
您使用“rw”模式打開檔案。這不是有效模式,請參閱以下檔案:https ://valadoc.org/glib-2.0/GLib.FileStream.open.html
Mode is used to determine the file access mode.
Mode: Meaning: Explanation: File already exists: File does not exist:
"r" read Open a file for reading read from start failure to open
"w" write Create a file for writing destroy contents create new
"a" append Append to a file write to end create new
"r " read extended Open a file for read/write read from start error
"w " write extended Create a file for read/write destroy contents create new
"a " append extended Open a file for read/write write to end create new
您可以使用 "w" 進行寫入或使用 "w " 進行讀取和寫入,與 "a" 和 "a " 相同,將保留現有檔案并寫入到最后。
uj5u.com熱心網友回復:
簡單的答案是使用w 而不是rw:
var file = FileStream.open("string.txt", "w ");
的檔案Filestream.open給出了接受的訪問模式的表格。
如果檔案不存在r模式,這rw似乎也被截斷,那么檔案無法打開并且斷言對我來說失敗。也Filestream.open不會拋出任何例外,因此會有關于無法訪問的 catch 子句的警告。IOStream如果您想撰寫與 GLib 的異步代碼主回圈集成的輸入/輸出代碼,您可能需要查看 GIO 。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/511805.html
標籤:文件瓦拉
