????1.C++???????????
#include <iostream>
#include <fstream>
using namespace std;
#define FILENAME "stat.dat"
int main()
{
fstream _file;
_file.open(FILENAME??ios::in);
if(!_file)
{
cout<<FILENAME<<"??б?????";
}
else
{
cout<<FILENAME<<"???????";
}
return 0;
}
????2.???? c ??????????
??????????: access
??????  ??: ??????????????
??????  ??: int access(const char *filename?? int amode);
???????????ù???????????????????????????????о??????????????ж?????????????????????????ò?????find??????????????????д????????е????????ж???????????????MSDN??
int_access(constchar*path??intmode);
ReturnValue
Eachofthesefunctionsreturns0ifthefilehasthegivenmode.Thefunctionreturns–1ifthenamedfiledoesnotexistorisnotaccessibleinthegivenmode;inthiscase??errnoissetasfollows:
EACCES
Accessdenied:file’spermissionsettingdoesnotallowspecifiedaccess.
ENOENT
Filenameorpathnotfound.
Parameters
path
Fileordirectorypath
mode
Permissionsetting
Remarks
Whenusedwithfiles??the_accessfunctiondetermineswhetherthespecifiedfileexistsandcanbeaccessedasspecifiedbythevalueofmode.Whenusedwithdirectories??_accessdeterminesonlywhetherthespecifieddirectoryexists;inWindowsNT??alldirectorieshavereadandwriteaccess.
modeValueChecksFileFor
00Existenceonly
02Writepermission
04Readpermission
06Readandwritepermission
Example
/*ACCESS.C:Thisexampleuses_accesstocheckthe
*filenamed"ACCESS.C"toseeifitexistsandif
*writingisallowed.
*/
#include<io.h>
#include<stdio.h>
#include<stdlib.h>
voidmain(void)
{
/*Checkforexistence*/
if((_access("ACCESS.C"??0))!=-1)
{
printf("FileACCESS.Cexists");
/*Checkforwritepermission*/
if((_access("ACCESS.C"??2))!=-1)
printf("FileACCESS.Chaswritepermission");
}
}
Output
FileACCESS.CexistsFileACCESS.Chaswritepermission