Directory, file and permissions
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)
Other info: none
To do: Playing with some of the file handling functions
To show: Directory, file, access right and permissions
/* Using the file handling functions */
#include <io.h>
#include <stdio.h
#include <stdlib.h>
#include <sys.h>
/* You can try other files and directories */
char obj[50] = "e:\\testfolder\\robots.txt";
char newobj[50] = "e:\\testfolder\\human.txt";
int main(void)
{
int result;
/* Check for existence */
printf("Check the file existence...\n");
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 */
printf("\nCheck for read/write permission...\n");
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 a file read-only */
printf("\nMake file read-only...\n");
if(_chmod(obj, _S_IREAD) == -1)
perror("File not found lol!");
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 */
printf("\nChange back to read/write...\n");
if(_chmod(obj, _S_IWRITE) == -1)
perror("file not found lol!");
else
{
printf("The 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 the file */
printf("\nAttempt to rename the file...\n");
result = rename(obj, newobj);
if(result != 0)
printf("Could not rename %s\n", obj);
else
printf("%s file has been renamed to\n %s\n", obj, newobj);
/* remove the created file */
printf("\nRemoving the created file...\n");
if(remove(newobj) == -1)
printf("Could not delete %s lol!\n", newobj);
else
printf("Ooops! %s file has been deleted lol!\n", newobj);
return 0;
}
Output example (when the file does not exist):
Check the file existence...
e:\testfolder\robots.txt file does not exist lol!
Check for read/write permission...
Make file read-only...
File not found lol!: No such file or directory
Change back to read/write...
file not found lol!: No such file or directory
Attempt to rename the file...
Could not rename e:\testfolder\robots.txt
Removing the created file...
Could not delete e:\testfolder\human.txt lol!
Press any key to continue . . .
Output example (when the file is exist):
Check the file existence...
e:\testfolder\robots.txt file exists
Check for read/write permission...
e:\testfolder\robots.txt file has write permission
e:\testfolder\robots.txt file has read permission
e:\testfolder\robots.txt file has write and read permission
Make file read-only...
The file mode is changed to read-only
e:\testfolder\robots.txt file has read permission
Change back to read/write...
The file's mode is changed to read/write
e:\testfolder\robots.txt file has write permission
Attempt to rename the file...
e:\testfolder\robots.txt file has been renamed to
e:\testfolder\human.txt
Removing the created file...
Ooops! e:\testfolder\human.txt file has been deleted lol!
Press any key to continue . . .