This is from Turbo C/C++, and answers some of your questions.
Code:
/*
access Determines accessibility of a file
Syntax:
int access(const char *filename, int amode);
Prototype in:
io.h
Remarks:
access checks the file named by filename to determine if it exists, and
whether it can be read, written to, or executed.
The list of amode values is as follows:
Value ณ Description
ออออออุอออออออออออออออออออออออออออออออออออออ
06 ณ Check for read and write permission
04 ณ Check for read permission
02 ณ Check for write permission
01 ณ Execute (ignored)
00 ณ Check for existence of file
Under DOS, all existing files have read access (amode equals 04), so 00 and
04 give the same result.
Similarly, amode values of 06 and 02 are equivalent because under DOS write
access implies read access.
If filename refers to a directory, access simply determines whether the
directory exists.
Return Value:
If the requested access is allowed, access returns 0.
Otherwise, it returns a value of -1, and the global variable errno is set to
one of the following:
Errno ณ
Setting ณ Description
อออออออออุออออออออออออออออออออออออออออ
ENOENT ณ Path or file name not found
EACCES ณ Permission denied
Portability:
access is available on UNIX systems.
See Also:
chmod fstat stat
Example:
#include <stdio.h>
#include <io.h>
int file_exists(char *filename);
int main(void)
{
printf("Does NOTEXIST.FIL exist: %s\n",
file_exists("NOTEXISTS.FIL") ? "YES" : "NO");
return 0;
}
int file_exists(char *filename)
{
return (access(filename, 0) == 0);
}
fstat Gets open file information.
Syntax:
int fstat(int handle, struct stat *statbuf);
Prototype in:
sys\stat.h
Remarks:
fstat stores information in the stat structure about the open file or
directory associated with handle.
statbuf points to the stat structure (defined in sys\stat.h). That structure
contains the following fields:
Field ณ Description
===============================================
st_mode ณ Bit mask giving information about the open file's mode
st_dev ณ Drive number of disk containing the file, or file handle
ณ if the file is on a device
st_rdev ณ Same as st_dev
st_nlink ณ Set to the integer constant 1
st_size ณ Size of the open file in bytes
st_atime ณ Most recent time the open file was modified
st_mtime ณ Same as st_atime
st_ctime ณ Same as st_atime
The stat structure contains three more fields not mentioned here. They
contain values that are not meaningful under DOS.
The bit mask that gives information about the mode of the open file includes
the following bits in the following two tables.
One of the following bits will be set:
Bit ณ Description
ออออออออออุออออออออออออออออออออออออออออออออออออออออออออออ
S_IFCHR ณ Set if handle refers to a device
S_IFREG ณ Set if an ordinary file is referred to by handle
One or both of the following bits will be set:
Bit ณ Description
ออออออออออุออออออออออออออออออออออออออออออออออออออออออออ
S_IWRITE ณ Set if user has permission to write to file
S_IREAD ณ Set if user has permission to read to file
The bit mask also includes the read/write bits. They are set according to
the file's permission mode.
Return Value:
fstat returns 0 if it has successfully retrieved the information about the
open file.
On error (failure to get the information), it returns -1 and sets the global
variable errno to EFADF (bad file handle).
Portability:
fstat is available on UNIX systems.
See Also:
access chmod stat
Example:
#include <sys\stat.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
struct stat statbuf;
FILE *stream;
/* open a file for update */
if ((stream = fopen("DUMMY.FIL", "w+"))
== NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return(1);
}
fprintf(stream, "This is a test");
fflush(stream);
/* get information about the file */
fstat(fileno(stream), &statbuf);
fclose(stream);
/* display the information returned */
if (statbuf.st_mode & S_IFCHR)
printf("Handle refers to a device.\n");
if (statbuf.st_mode & S_IFREG)
printf("Handle refers to an ordinary file.\n");
if (statbuf.st_mode & S_IREAD)
printf("User has read permission on file.\n");
if (statbuf.st_mode & S_IWRITE)
printf("User has write permission on file.\n");
printf("Drive letter of file: %c\n", 'A'+statbuf.st_dev);
printf("Size of file in bytes: %ld\n", statbuf.st_size);
printf("Time file last opened: %s\n", ctime(&statbuf.st_ctime));
return 0;
}