|< C Run-Time 2 | Main | C Run-Time 4 >| Site Index | Download |


 

 

 

 

MODULE B

IMPLEMENTATION SPECIFIC

MICROSOFT C Run-Time 3

 

 

 

 

What are in this Module?

  1. File-Handling Functions (Open File)

  2. The File Permission mask

  3. The File Statistic

  4. Program Examples

 

 

 

 

 

 

 

 

 

 

 

 

 

My Training Period: xx hours. Before you begin, read some instruction here. Functions and structure used in the program examples were dumped at Win32 functions & structures.

 

Abilities that supposed to be acquired:

  • Able to recognize the standard and implementation specific C.

  • Able to find and collect the required and related information in order to use functions to accomplish your tasks.

  • Able to understand and use functions that available in the libraries in your own programs.

File-Handling Functions (Open File)

  • The following functions used to open files.

Function

Use

fopen()

Opens a file and returns a pointer to the open file.

_fsopen()

Open a stream with file sharing and returns a pointer to the open file.

_open()

Opens a file and returns a file handle to the opened file.

_sopen()

Open a file with file sharing and returns a file handle to the open file.

_fdopen()

Associates a stream with a file that was previously opened for low-level I/O and returns a pointer to the open stream.

_fileno()

Gets the file handle associated with a stream and returns the file handle.

_open_osfhandle()

Associates a C run-time file handle with an existing operating-system file handle and returns a C run-time file handle.

_pipe()

Creates a pipe for reading and writing.

freopen()

Reassign a file pointer.

 

Table 1:  File opening functions.

  • The following Table is an example of the needed information for fopen() and wfopen() functions.

Information

Description

The function

fopen(), wfopen().

The use

Open a file.

The prototype

FILE *fopen( const char *filename, const char *mode);
/* for wide character*/
FILE *_wfopen( const wchar_t *filename, const wchar_t *mode);

Example

char fname[30] = "C:\\testfile.txt";

fopen(fname, "r");

The parameters

filename – The filename.

mode - Type of access permitted.

The return value

Each of these functions returns a pointer to the open file.  A null pointer value indicates an error.

The header file

<stdio.h> or <wchar.h> (for _wopen())

 

Table 2:  fopen() and wfopen() function information.

 

mode

"r"

Opens for reading. If the file does not exist or cannot be found, the fopen() call fails.

"w"

Opens an empty file for writing.  If the given file exists, its contents are destroyed.

"a"

Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn't exist.

"r+"

Opens for both reading and writing, the file must exist.

"w+"

Opens an empty file for both reading and writing.  If the given file exists, its contents are destroyed.

"a+"

Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn't exist.

The following characters can be included in mode to specify the translation mode for newline characters:

t

Open in text (translated) mode. In this mode, CTRL+Z is interpreted as an end-of-file character on input.  In files opened for reading/writing with "a+", fopen() checks for a CTRL+Z at the end of the file and removes it, if possible.  This is done because using fseek() and ftell() to move within a file that ends with a CTRL+Z, may cause fseek() to behave improperly near the end of the file.

b

Open in binary (un-translated) mode; translations involving carriage-return and linefeed characters are suppressed.

c

Enable the commit flag for the associated filename so that the contents of the file buffer are written directly to disk if either fflush() or _flushall() is called.

n

Reset the commit flag for the associated filename to "no-commit".  This is the default.  It also overrides the global commit flag if you link your program with commode.obj. The global commit flag default is "no-commit" unless you explicitly link your program with commode.obj.

S

Specifies that caching is optimized for, but not restricted to, sequential access from disk.

R

Specifies that caching is optimized for, but not restricted to, random access from disk.

T

Specifies a file as temporary.  If possible, it is not flushed to disk.

D

Specifies a file as temporary.  It is deleted when the last file pointer is closed.

 

Table 3:  File type of access, mode.

Information

Description

The function

_sopen(), _wsopen().

The use

Open a file for sharing.

The prototype

int _sopen( const char *filename, int oflag, int shflag [, int pmode ] );
 /* for wide character */
int _wsopen( const wchar_t *filename, int oflag, int shflag [, int pmode ] );

Example

char fname[50] = C:\\testfile.txt;

int _sopen(fname, _O_CREAT, _SH_DENYNO, _S_IWRITE);

The parameters

filename – The filename.

oflag - Type of operations allowed.

shflag - Type of sharing allowed.

pmode - Permission setting.

The return value

Each of these functions returns a file descriptor for the opened file.  A return value of –1 indicates an error, in which case errno is set to one of the following values:

  1. EACCES - Given path is a directory, or file is read-only, but an open-for-writing operation was attempted.

  2. EEXIST - _O_CREAT and _O_EXCL flags were specified, but filename already exists.

  3. EINVAL - Invalid oflag or shflag argument.

  4. EMFILE - No more file descriptors available.

  5. ENOENT - File or path not found.

The header file

<io.h> or <wchar.h> (for _wsopen())

 

Table 4:  _sopen() and _wsopen() functions information.

oflag

Description

_O_APPEND

Moves file pointer to end of file before every write operation.

_O_BINARY

Opens file in binary (un-translated) mode.

_O_CREAT

Creates and opens new file for writing. Has no effect if file specified by filename exists.  pmode argument is required when _O_CREAT is specified.

_O_CREAT | _O_SHORT_LIVED

Create file as temporary and if possible do not flush to disk.  pmode argument is required when _O_CREAT is specified.

_O_CREAT | _O_TEMPORARY

Create file as temporary; file is deleted when last file handle is closed.  pmode argument is required when _O_CREAT is specified.

_O_CREAT | _O_EXCL

Returns error value if file specified by filename exists.  Applies only when used with _O_CREAT.

_O_RANDOM

Specifies that caching is optimized for, but not restricted to, random access from disk.

_O_RDONLY

Opens file for reading only; cannot be specified with _O_RDWR or _O_WRONLY.

_O_RDWR

Opens file for both reading and writing; you cannot specify this flag with _O_RDONLY or _O_WRONLY.

_O_SEQUENTIAL

Specifies that caching is optimized for, but not restricted to, sequential access from disk.

_O_TEXT

Opens file in text (translated) mode.

_O_TRUNC

Opens file and truncates it to zero length; file must have write permission. You cannot specify this flag with _O_RDONLY. _O_TRUNC used with _O_CREAT opens an existing file or creates a new file.  The _O_TRUNC flag destroys the contents of the specified file.

_O_WRONLY

Opens file for writing only; cannot be specified with _O_RDONLY or _O_RDWR.

 

Table 5:  oflag constants.

shflag

Description

_SH_DENYRW

Denies read and write access to file.

_SH_DENYWR

Denies write access to file.

_SH_DENYRD

Denies read access to file.

_SH_DENYNO

Permits read and write access.

 

Table 6:  shflag.

pmode

Description

_S_IREAD

Reading only permitted.

_S_IWRITE

Writing permitted (effectively permits reading and writing).

_S_IREAD | _S_IWRITE

Reading and writing permitted.

 

Table 7:  pmode.

Information

Description

The function

_open().

The use

Open a file.

The prototype

int _open(const char *filename, int oflag [, int pmode]);

[ ... ] – denotes optional.

Example

char fname[50] = C:\\testfile.txt;

int _sopen(fname, _O_CREAT, _SH_DENYNO, _S_IWRITE);

The parameters

filename – The filename.

oflag - Type of operations allowed.

pmode - Permission mode.

The return value

Each of these functions returns a file handle for the opened file.  A return value of –1 indicates an error, in which case errno is set to one of the following values:

  1. EACCES - Tried to open read-only file for writing, or file's sharing mode does not allow specified operations, or given path is directory.

  2. EEXIST - _O_CREAT and _O_EXCL flags specified, but filename already exists.

  3. EINVAL - Invalid oflag or pmode argument.

  4. EMFILE - No more file handles available (too many open files).

  5. ENOENT - File or path not found.

The header file

<io.h>

 

Table 8:  _open() function information.

/* playing with some of the file handling functions */

#include <io.h>

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/stat.h>

 

/* you can try other files and directories, change accordingly to yours */

char obj[50] = "d:\\test\\testsubtwo\\testsubthree\\robots.txt";

char newobj[50] = "d:\\test\\testsubtwo\\testsubthree\\human.txt";

 

int main()

{

    /* check for existence */

    if((_access(obj, 0)) != -1)

      printf("%s file exists\n", obj);

    else

       printf("%s file does not exist lol!\n", obj);

        /* check for read/write permission */

    if((_access(obj, 2)) != -1)

        printf("%s file has write permission\n", obj);

    if((_access(obj, 4)) != -1)

        printf("%s file has read permission\n", obj);

    if((_access(obj, 6)) != -1)

        printf("%s file has write and read permission\n\n", obj);

        /* make file read-only: */

    if(_chmod(obj, _S_IREAD) == -1)

        perror("File not found lol!\n");

    else

    {

         printf("The file mode is changed to read-only\n");

         _chmod(obj, _S_IREAD);

    }

    if((_access(obj, 4)) != -1)

         printf("%s file has read permission\n", obj);

        /* change back to read/write */

    if(_chmod(obj, _S_IWRITE) == -1)

         perror("file not found lol!\n");

    else

    {

         printf("\nThe file\'s mode is changed to read/write\n");

         _chmod(obj, _S_IWRITE);

    }

        if((_access(obj, 2)) != -1)

         printf("%s file has write permission\n", obj);

    /* attempt to rename file */

    int result = rename(obj, newobj);

    if(result != 0)

          printf("\nCould not rename %s\n", obj);

    else

          printf("\n%s file has been renamed to\n %s\n", obj, newobj);

    /* remove the file */

    if(remove(newobj) == -1)

          printf("\nCould not delete %s lol!\n", newobj);

    else

          printf("\nOoops! %s file has been deleted lol!\n", newobj);

    return 0;

}

A sample output:

 

d:\test\testsubtwo\testsubthree\robots.txt file exists

d:\test\testsubtwo\testsubthree\robots.txt file has write permission

d:\test\testsubtwo\testsubthree\robots.txt file has read permission

d:\test\testsubtwo\testsubthree\robots.txt file has write and read permission

 

The file mode is changed to read-only

d:\test\testsubtwo\testsubthree\robots.txt file has read permission

 

The file's mode is changed to read/write

d:\test\testsubtwo\testsubthree\robots.txt file has write permission

 

d:\test\testsubtwo\testsubthree\robots.txt file has been renamed to

 d:\test\testsubtwo\testsubthree\human.txt

 

Ooops! d:\test\testsubtwo\testsubthree\human.txt file has been deleted lol!

Press any key to continue

 

The File Permission mask

Information

Description

The function

_umask().

The use

Sets the default file-permission mask.

The prototype

int _umask(int pmode);

Example

_umask(_S_IWRITE);

The parameters

pmode - default permission setting - _S_IREAD_S_IWRITE.

The return value

Returns the previous value of pmode. There is no error return.

The header files

<io.h>, <sys/stat.h> and <sys/types.h>.

 

Table 9:  _umask() function information.

/* this program uses _umask to set the file-permission mask so that all future files will be created as read-only files.

Then we create a file for write and test opening the file for writing */

#include <fcntl.h>

#include <sys/stat.h>

#include <sys/types.h>

#include <io.h>

#include <stdio.h>

 

int main()

{

    int fh, oldmask;

    /* file in current working directory,  change accordingly to yours */

    char test[20] = "test.txt";

        /* create read-only files: */

    oldmask = _umask(_S_IWRITE);

    printf("Oldmask = 0x%.4x\n", oldmask);

        /*create a file with write permission */

    fh = _creat(test, _S_IWRITE);

    if(fh == -1)

      printf("Couldn't create %s file!\n", test);

   else

   {

      printf("%s file successfully created.\n", test);

      _close(fh);

   }

    /* try opening file for write */

    fh = _open(test, _O_WRONLY);

    if(fh == -1)

         printf("%s opening failed!\n", test);

    else

   {

      printf("%s opening succeeded!\n", test);

      if(_close(fh) == 0)

          printf("%s closed successfully!\n", test);

   }

  printf("Oldmask = 0x%.4x\n", oldmask);

  return 0;

}

A sample output:

 

Oldmask = 0x0000

test.txt file successfully created.

test.txt opening failed!

Oldmask = 0x0000

Press any key to continue

/* this program uses _umask to set the file-permission mask so that all future files will be created as read-only files.

Then we create a file for write and test opening the file for writing */

#include <fcntl.h>

#include <sys/stat.h>

#include <sys/types.h>

#include <io.h>

#include <stdio.h>

#include <share.h> // _SH_RDWR

 

int main()

{

    int fh, oldmask;

    /* file in root directory, change accordingly to yours */

    char test[20] = "C:\\test.txt";

    /* create read-only files: */

    _umask_s(_S_IWRITE, &oldmask);

    /* oldmask = _umask(_S_IWRITE); */

    printf("Oldmask = 0x%.4x\n", oldmask);

    /* create a file with write permission */

    _sopen_s( &fh, test, _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE );

    /* fh = _creat(test, _S_IWRITE); */

    if(fh == -1)

        printf("Couldn't create %s file!\n", test);

    else

    {

        printf("%s file successfully created.\n", test);

        _close(fh);

    }

    /* try opening file for write */

    /* fh = _open(test, _O_WRONLY); */

    _sopen_s( &fh, test, _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE );

    if(fh == -1)

        printf("%s opening failed!\n", test);

    else

    {

        printf("%s opening succeeded!\n", test);

        if(_close(fh) == 0)

            printf("%s closed successfully!\n", test);

    }

    printf("Oldmask = 0x%.4x\n", oldmask);

    return 0;

}

 

A sample output:

 

The File Statistic

Information

Description

The function

_stat(), _stat64(), _stati64(), _wstat(), _wstat64(), _wstati64()

The use

Get status information on a file.

The prototype

int _stat(const char *path, struct _stat *buffer);
int _stat64(const char *path, struct __stat64 *buffer);
int _stati64(const char *path, struct _stati64 *buffer);
int _wstat(const wchar_t *path, struct _stat *buffer);
int _wstat64(const wchar_t *path, struct __stat64 *buffer);

int _wstati64(const wchar_t *path, struct _stati64 *buffer);

Example

struct __stat64 buf;

char fname[50] = "c:\\WINNT\\system32\\config\\sam.log";

_stat64(fname, &buf);

The parameters

path - Pointer to a string containing the path of existing file.

buffer - Pointer to structure that stores results.

The return value

Each of these functions returns 0 if the file-status information is obtained.  A return value of –1 indicates an error, in which case errno is set to ENOENT, indicating that the filename or path could not be found. The date stamp on a file can be represented if it is later than midnight, January 1, 1970, and before 19:14:07 January 18, 2038, UTC unless you use _stat64() or _wstat64(), in which case the date can be represented up till 23:59:59, December 31, 3000, UTC.

The header files

_stat(), _stat64(), _stati64() -  <sys/types.h> followed by <sys/stat.h>.

_wstat(), _wstat64(), _wstati64() -  <sys/types.h> followed by <sys/stat.h> or <wchar.h>.

 

Table 10:  File statistic functions information.

Element

Description

st_gid

Numeric identifier of group that owns file (UNIX-specific) This field will always be zero on Windows NT systems.  A redirected file is classified as a Windows NT file.

st_atime

Time of last access of file. Valid on NTFS but not on FAT formatted disk drives.

st_ctime

Time of creation of file. Valid on NTFS but not on FAT formatted disk drives.

st_dev

Drive number of the disk containing the file (same as st_rdev).

st_ino

Number of the information node (the inode) for the file (UNIX-specific).  On UNIX file systems, the inode describes the file date and time stamps, permissions, and content. When files are hard-linked to one another, they share the same inode.  The inode, and therefore st_ino, has no meaning in the FAT, HPFS, or NTFS file systems.

st_mode

Bit mask for file-mode information.  The _S_IFDIR bit is set if path specifies a directory; the _S_IFREG bit is set if path specifies an ordinary file or a device. User read/write bits are set according to the file's permission mode; user execute bits are set according to the filename extension.

st_mtime

Time of last modification of file.

st_nlink

Always 1 on non-NTFS file systems.

st_rdev

Drive number of the disk containing the file (same as st_dev).

st_size

Size of the file in bytes; a 64-bit integer for _stati64() and _wstati64().

st_uid

Numeric identifier of user who owns file (UNIX-specific). This field will always be zero on Windows NT systems. A redirected file is classified as a Windows NT file.

 

Table 11:  _stat structure members.

/* using the _stat64 function reporting a file information */

#include <time.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <stdio.h>

 

int main()

{

    struct __stat64 buf;

    int result;

    char fname[50] = "c:\\WINNT\\system32\\config\\sam.log";

        /* get data associated with a file */

    result = _stat64(fname, &buf);

        /* test if statistics are valid */

    if(result != 0)

         printf("Problem getting %s file information.\n", fname);

    else

    {

        /*dump some of the file information*/

        /*Notice how the structures' elements were accessed*/

        printf("The file     : %s\n", fname);

        printf("Drive        : %c:\n", buf.st_dev + 'A');

        printf("File size    : %ld bytes\n", buf.st_size);

        printf("Time created : %s", _ctime64(&buf.st_ctime));

        printf("Last accessed       : %s", _ctime64(&buf.st_atime));

        printf("Time modified       : %s", _ctime64(&buf.st_mtime));

        printf("Bit mask     : %p\n", &buf. st_mode);

        return 0;

    }

}

 

A sample output:

The file        : c:\WINNT\system32\config\sam.log
Drive           : C:
File size       : 1024 bytes
Time created    : Sat Jun 05 07:07:16 2004
Last accessed   : Wed May 18 20:45:12 2005
Time modified   : Wed May 18 20:45:12 2005
Bit mask        : 0012FEA6
Press any key to continue

/* using the _stat64 function reporting a file information */

#include <time.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <stdio.h>

 

int main()

{

    struct __stat64 buf;

    int result;

    // change accordingly to other path

    char fname[50] = "c:\\Windows\\system32\\config\\sam.log";

    char buff[50] = "";

    /* get data associated with a file */

    result = _stat64(fname, &buf);

    /* test if statistics are valid */

    if(result != 0)

        printf("Problem getting %s file information.\n", fname);

    else

        {

            /*dump some of the file information*/

            /*Notice how the structures' elements were accessed*/

            printf("The file : %s\n", fname);

            printf("Drive : %c:\n", buf.st_dev + 'A');

            printf("File size : %ld bytes\n", buf.st_size);

            /* ctime_s( char* buffer, size_t sizeInBytes, const time_t *time); */

            _ctime64_s(buff, 50, &buf.st_ctime);

            printf("Time created : %s", buff);

            _ctime64_s(buff, 50, &buf.st_atime);

            printf("Last accessed : %s", buff);

            _ctime64_s(buff, 50, &buf.st_mtime);

            printf("Time modified : %s", buff);

            printf("Bit mask : %p\n", &buf. st_mode);

            return 0;

        }

}

 

A sample output:

/* opening file for input and output, then close. Running on 32 bits system */

#include <fcntl.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <io.h>

#include <stdio.h>

 

int main()

{

    int  fhand1, fhand2;

    char fname1[50] = "cruntime.cpp";

    char fname2[50] = "robots.txt";

 

fhand1 = _open(fname1, _O_RDONLY);

if(fhand1 == -1)

    printf("opening %s failed for input\n", fname1);

else

{

  printf("%s opening succeeded for input\n", fname1);

  if(_close(fhand1) == 0)

      printf("%s closed successfully!\n", fname1);

}

 

fhand2 = _open(fname2, _O_WRONLY | _O_CREAT, _S_IREAD | _S_IWRITE);

if(fhand2 == -1)

  printf("%s open failed for output\n", fname2);

else

{

  printf("%s open succeeded for output\n", fname2);

  if(_close(fhand2) ==0)

      printf("%s closed successfully!\n", fname1);

}

return 0;

}

A sample output:

cruntime.cpp opening succeeded for input

cruntime.cpp closed successfully!

robots.txt open succeeded for output

cruntime.cpp closed successfully!

Press any key to continue

 

/* opening file for input and output, then close. Running on 32 bits system */

#include <fcntl.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <io.h>

#include <stdio.h>

#include <share.h> // _O_RDWR

 

int main()

{

    int fhand1, fhand2;

    /* change accordingly to your other path */

    char fname1[50] = "testwin32.cpp";

    char fname2[50] = "C:\\VBPTask.log";

    /* fhand1 = _open(fname1, _O_RDONLY); */

    _sopen_s( &fhand1, fname1, _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE );

    if(fhand1 == -1)

        printf("opening %s failed for input\n", fname1);

    else

    {

        printf("%s opening succeeded for input\n", fname1);

        if(_close(fhand1) == 0)

            printf("%s closed successfully!\n", fname1);

    }

    /* fhand2 = _open(fname2, _O_WRONLY | _O_CREAT, _S_IREAD | _S_IWRITE); */

    _sopen_s( &fhand2, fname2, _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE );

    if(fhand2 == -1)

        printf("%s open failed for output\n", fname2);

    else

    {

        printf("%s open succeeded for output\n", fname2);

        if(_close(fhand2) == 0)

            printf("%s closed successfully!\n", fname1);

    }

    return 0;

}

 

A sample output:

 

/* playing with directory, file and permission */

#include <stdio.h>

#include <stdlib.h>

#include <direct.h>

#include <io.h>

#include <time.h>

#include <sys/types.h>

#include <sys/stat.h>

 

int main(void)

{

int  curdrive;

char buffer[_MAX_PATH];

/* file name */

char fname[20] = "content.doc";

/* path */

char dir[50] = "d:\\test\\testsubtwo\\testsubthree";

struct _finddata_t c_file;

/* file handle */

long hFile;

 

/* ---------------------------------------------- */

printf("Firstly, get the current drive...\n");

curdrive = _getdrive();

printf("Current drive is %c:\n\n", curdrive + 'A'-1);

 

/* ----------------------------------------------- */

printf("Next, get the current working directory...\n");

printf("Current working directory is: \n");

if(_getcwd(buffer, _MAX_PATH) == NULL)

perror("_getcwd error lol!");

else

printf("%s\n\n", buffer);

 

/*----------------------------------------------- */

printf("Change the current working directory...\n");

_chdir(dir);

printf("\n");

 

/*------------------------------------------------ */

printf("Current working directory is: \n");

if(_getcwd(buffer, _MAX_PATH) == NULL)

perror("_getcwd error");

else

printf("%s\n\n", buffer);

 

/* ------------------------------------------------ */

hFile = _findfirst("*.*", &c_file);

{

 printf("Listing of *.* files\n\n");

 printf("\nRDO HID SYS ARC  TYPE  FILE             DATE %19c SIZE\n", ' ');

 printf("--- --- --- ---  ----  ----             ---- %19c ----\n", ' ');

 printf((c_file.attrib & _A_RDONLY) ? " Y  " : " N  ");

 printf((c_file.attrib & _A_HIDDEN) ? " Y  " : " N  ");

 printf((c_file.attrib & _A_SYSTEM) ? " Y  " : " N  ");

 printf((c_file.attrib & _A_ARCH)   ? " Y  " : " N  ");

 printf((c_file.attrib & _A_NORMAL) ? " Y  " : " N  ");

 printf("   %-15s %.24s  %6d\n", c_file.name, ctime(&(c_file.time_write)), c_file.size );

 

 /* find the rest of the files */

 while(_findnext(hFile, &c_file) == 0)

 {

  printf((c_file.attrib & _A_RDONLY) ? " Y  " : " N  ");

  printf((c_file.attrib & _A_HIDDEN) ? " Y  " : " N  ");

  printf((c_file.attrib & _A_SYSTEM) ? " Y  " : " N  ");

  printf((c_file.attrib & _A_ARCH)   ? " Y  " : " N  ");

  printf((c_file.attrib & _A_NORMAL) ? " Y  " : " N  ");

  printf("   %-15s %.24s  %6d\n", c_file.name, ctime(&(c_file.time_write)), c_file.size);

 }

 _findclose(hFile);

}

/* check the file exist... */

if((_access(fname, 0)) != -1)

  printf("\n%s file exists\n", fname);

else

{

 printf("\n%s file not found lor!\n", fname);

 /* just exit */

 exit(1);

}

 

/* check the permission */

printf("\nTest the permission for %s file\n", fname);

 

/* if write, then change to read.  Must check the write first

because file that can be write can also be read but not vice versa */

if((_access(fname, 2)) == 0)

{

  printf("\n%s file has write permission!\n", fname);

  printf("\nChange to read only permission...\n");

  _chmod(fname, _S_IREAD);

 

  printf("\nRetest the read only permission...\n");

  if((_access(fname, 4)) == 0)

  printf("%s file is READ ONLY!\n", fname);

  exit(0);

}

 

/* if not write it should be read permission, then change to write permission*/

else

{

printf("\n%s file is READ ONLY!\n", fname);

printf("\nChange to write permission...\n");

_chmod(fname, _S_IWRITE);

 

printf("\nRetest for write permission...");

if((_access(fname, 2)) == 0)

 printf("\n%s has write permission!\n", fname);

exit(0);

}

return 0;

}

 

A sample output:

Firstly, get the current drive...

Current drive is D:

 

Next, get the current working directory...

Current working directory is:

d:\mcruntime

 

Change the current working directory...

 

Current working directory is:

d:\test\testsubtwo\testsubthree

 

Listing of *.* files

 

RDO HID SYS ARC  TYPE  FILE             DATE                     SIZE

--- --- --- ---  ----  ----             ----                     ----

 N   N   N   N   N     .               Mon May 16 22:10:30 2005       0

 N   N   N   N   N     ..              Mon May 16 22:10:30 2005       0

 N   N   N   Y   N     content.doc     Sun Sep 19 12:19:25 2004  264192

 Y   N   N   Y   N     module32.html   Tue Jun 01 22:27:12 2004  375168

 Y   N   N   N   N     module32_files  Fri May 13 22:53:07 2005       0

 N   N   N   Y   N     paass.xls       Fri Oct 29 23:02:26 2004   21504

 Y   N   N   Y   N     preface.txt     Thu Aug 12 15:45:38 2004     906

 N   N   N   Y   N     robots.txt      Sun Oct 03 20:23:50 2004    3138

 

content.doc file exists

 

Test the permission for content.doc file

 

content.doc file has write permission!

 

Change to read only permission...

 

Retest the read only permission...

content.doc file is READ ONLY!

Press any key to continue

Windows file property page

 

Figure 1

Firstly, get the current drive...

Current drive is D:

 

Next, get the current working directory...

Current working directory is:

d:\mcruntime

 

Change the current working directory...

 

Current working directory is:

d:\test\testsubtwo\testsubthree

 

Listing of *.* files

 

RDO HID SYS ARC  TYPE  FILE             DATE                     SIZE

--- --- --- ---  ----  ----             ----                     ----

 N   N   N   N   N     .               Mon May 16 22:10:30 2005       0

 N   N   N   N   N     ..              Mon May 16 22:10:30 2005       0

 Y   N   N   Y   N     content.doc     Sun Sep 19 12:19:25 2004  264192

 Y   N   N   Y   N     module32.html   Tue Jun 01 22:27:12 2004  375168

 Y   N   N   N   N     module32_files  Fri May 13 22:53:07 2005       0

 N   N   N   Y   N     paass.xls       Fri Oct 29 23:02:26 2004   21504

 Y   N   N   Y   N     preface.txt     Thu Aug 12 15:45:38 2004     906

 N   N   N   Y   N     robots.txt      Sun Oct 03 20:23:50 2004    3138

 

content.doc file exists

 

Test the permission for content.doc file

 

content.doc file is READ ONLY!

 

Change to write permission...

 

Retest for write permission...

content.doc has write permission!

Press any key to continue

Windows file property page

 

Figure 2

/* playing with directory, file and permission */

#include <stdio.h>

#include <stdlib.h>

#include <direct.h>

#include <io.h>

#include <time.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <windows.h> /* GetLastError() */

 

int main(void)

{

    int curdrive;

    char buffer[_MAX_PATH];

    /* file name, change accordingly to other file */

    char fname[50] = "test.doc";

    char buff[50] = "";

    /* path, change accordingly to other path available on your machine */

    char dir[50] = "D:\\mycd1\\Ethertest8712";

    struct _finddata_t c_file;

    /* file handle */

    intptr_t hFile;

    /* ---------------------------------------------- */

    printf("Firstly, get the current drive...\n");

    curdrive = _getdrive();
    printf("Current drive is %c:\n\n", curdrive + 'A'-1);

    /* ----------------------------------------------- */

    printf("Next, get the current working directory...\n");

    printf("Current working directory is: \n");

    if(_getcwd(buffer, _MAX_PATH) == NULL)

        perror("_getcwd() error lol!");

    else

        printf("%s\n\n", buffer);

    /*----------------------------------------------- */

    printf("Change the current working directory...\n");

    _chdir(dir);

    printf("\n");

    /*------------------------------------------------ */

    printf("Current working directory is: \n");

    if(_getcwd(buffer, _MAX_PATH) == NULL)

        perror("_getcwd() error");

    else

        printf("%s\n\n", buffer);

    /* ------------------------------------------------ */

    hFile = _findfirst("*.*", &c_file);

    {

        printf("Listing of *.* files\n\n");

        printf("\nRDO HID SYS ARC TYPE FILE DATE %19c SIZE\n", ' ');

        printf("--- --- --- --- ---- ---- ---- %19c ----\n", ' ');

        printf((c_file.attrib & _A_RDONLY) ? " Y " : " N ");

        printf((c_file.attrib & _A_HIDDEN) ? " Y " : " N ");

        printf((c_file.attrib & _A_SYSTEM) ? " Y " : " N ");

        printf((c_file.attrib & _A_ARCH) ? " Y " : " N ");

        printf((c_file.attrib & _A_NORMAL) ? " Y " : " N ");

        ctime_s(buff, 50, &(c_file.time_write));

        printf(" %-15s %.24s %6d\n", c_file.name, buff, c_file.size );

        /* find the rest of the files */

        while(_findnext(hFile, &c_file) == 0)

        {

            printf((c_file.attrib & _A_RDONLY) ? " Y " : " N ");

            printf((c_file.attrib & _A_HIDDEN) ? " Y " : " N ");

            printf((c_file.attrib & _A_SYSTEM) ? " Y " : " N ");

            printf((c_file.attrib & _A_ARCH) ? " Y " : " N ");

            printf((c_file.attrib & _A_NORMAL) ? " Y " : " N ");

            ctime_s(buff, 50, &(c_file.time_write));

            printf(" %-15s %.24s %6d\n", c_file.name, buff, c_file.size);

        }

    _findclose(hFile);

    }

    /* check the file existence... 00 - Existence only, 02 - Write-only, 04 - Read-only, 06 - Read and write */

    if((_access(fname, 0)) != -1)

        printf("\n%s file exists\n", fname);

    else

    {

        printf("\n%s file not found lor! Error: %d\n", fname, GetLastError());

        /* just exit */

        exit(1);

    }

    /* check the permission */

    printf("\nTest the permission for %s file\n", fname);

   

    /* if write, then change to read. Must check the write first because file that can be write can also be read but not vice versa */

    if((_access(fname, 2)) == 0)

    {

        printf("\n%s file has write permission!\n", fname);

        printf("\nChange to read only permission...\n");

        _chmod(fname, _S_IREAD);

        printf("\nRetest the read only permission...\n");

        if((_access(fname, 4)) == 0)

            printf("%s file is READ ONLY!\n", fname);

        exit(0);

    }

    /* if not write it should be read permission, then change to write permission*/

    else

    {

        printf("\n%s file is READ ONLY!\n", fname);

        printf("\nChange to write permission...\n");

        _chmod(fname, _S_IWRITE);

        printf("\nRetest for write permission...");

        if((_access(fname, 2)) == 0)

            printf("\n%s has write permission!\n", fname);

        exit(0);

    }

    return 0;

}

 

A sample output output:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Further reading and digging:

 

  1. Check the best selling C, C++ and Windows books at Amazon.com.

  2. Microsoft Visual C++, online MSDN.

  3. For Multibytes, Unicode characters and Localization please refer to Locale, Wide Character & Unicode (Story) and Windows Users & Groups tutorial (Implementation).

  4. Notation used in MSDN is Hungarian Notation instead of CamelCase and is discussed in C & C++ Notations.

 

 

 

 

 

 

 

|< C Run-Time 2 | Main | C Run-Time 4 >| Site Index | Download |

Some Old Part of the C-Run Time: Part 1 | Part 2 | Part 3 | Part 4


 

C & C++ Programming Tutorial | C Programming Practice