[英]How to create an empty file in linux using c++
我想用 c++ 在 ubuntu 中創建一個空文件,我嘗試了很多方法,但它一直失敗並顯示此錯誤
錯誤:沒有與 'std::basic_ofstream::open(std::cxxll::...
我的代碼:
ifstream f(saltFile.c_str());
if (f.good())
{
cout << saltFile + " file already existed" << endl;
}
else
{
ofstream file;
file.open (saltFile, ios::out);
cout << saltFile << " created!" << endl;
}
如果您有 C++11 或更高版本的編譯器,您當然可以使用:
else
{
ofstream file;
file.open (saltFile, ios::out);
cout << saltFile << " created!" << endl;
}
如果您有 C++11 之前的編譯器,則需要稍微調整對open()
的調用。
file.open (saltFile.c_str(), ios::out);
創建一個文件,你可以使用 ofstream
#include <fstream>
int main() {
std::ofstream outfile;
outfile.open("test.txt", std::ios_base::app); // append instead of overwrite
outfile << "Data";
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.