|
发表于 2009-11-6 00:42:45
|
显示全部楼层
IP:江苏扬州
fstream fs(“fname”, ios_base::in); // attempt open for readif
(!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 readif
(!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
} |
|