fstream fs(“fname”, ios_base::in);// attempt open for read
if (!fs)
{
// file doesn't exist; don't create a new one
}
else //ok, file exists. close and reopen in write mode
{
fs.close();
fs.open(“fname”, ios_base::out); // reopen for write
}
You can just do the opposite for ios::noreplace:
fstream fs(“fname”, ios_base::in);// attempt open for read
if (!fs)
{
// file doesn't exist; create a new one
fs.open(“fname”, ios_base::out);
}
else //ok, file exists; close and reopen in write mode
{
fs.close()
fs.open(“fname”, ios_base::out); // reopen for write
}
可以用这种方法判断文件是否存在
vc++ ios::in ios::out 含义
最新推荐文章于 2023-01-03 17:19:23 发布