Sie sind auf Seite 1von 4

#include <stdio.

h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
openfile()
{
int pfd=0;
char file[100]="";
char location[100]="";
printf("Enter file name: ");
scanf("%s",file);
printf("Enter file location, example= '/home/ivana/' :");
scanf("%s",location);
strcat( location, file);
if ((pfd = open(location, O_WRONLY | O_CREAT |
O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))
== -1)
{
perror("Cannot create output file\n");
}
else
{
printf("n%s file create successful\n",file);
}
close (pfd);
}
filepermission()
{
int pfd=0;
char file[100]="";
char location[100]="";
char mode[4];
int i;
printf("Enter file name: ");
scanf("%s",file);
printf("Enter file location, example= '/home/ivana/' :");
scanf("%s",location);
strcat( location, file);
printf("Permission :\n");
printf("read
= 4\n");
printf("write
= 2\n");
printf("execute = 1\n");
printf("---------------------------\n");
printf("Example :\n");
printf("owner
= read | write = 6\n");
printf("group
= read
= 4\n");
printf("other
= read
= 4\n");
printf("Choose permission :0644\n");
printf("---------------------------\n");
printf("Choose permission :");

scanf("%s",mode);
i = strtol(mode, 0, 8);
chmod(location,i) ;
printf("Permission have change\n");
}
readfile() {
char file[100];
char location[100];
printf("Enter file name: ");
scanf("%s",file);
printf("Enter file location, example= '/home/ivana/' :");
scanf("%s",location);
strcat( location, file);
char str[70];
FILE *p;
if((p=fopen(location,"r"))==NULL){
printf("\nUnable to open file %s\n",file);
}
printf("\nFilenname : %s\n",file);
while(fgets(str,70,p)!=NULL)
puts(str);
fclose(p);
}
removefile() {
char file[100];
char location[100];
int status;
printf("Enter file name: ");
scanf("%s",file);
printf("Enter file location, example= '/home/ivana/' :");
scanf("%s",location);
strcat( location, file);
status = remove(location);
if( status == 0 )
printf("%s file deleted successfully.\n",file);
else
{
printf("Unable to delete the file\n");
perror("Error");
}
}
listfile()
{
DIR *dir = opendir(".");

if (dir) {
struct dirent *s_dir;
while((s_dir = readdir(dir))) {
printf("%s \n", s_dir->d_name);
}
printf("\n");
}
}
void main()
{
int select=0, choice;
operation1:
do
{
system("clear");
printf("File Operations\n");
printf("1- Create/Open File\n");
printf("2- Change file permission\n");
printf("3- Read the file\n");
printf("4- Delete/Remove file\n");
printf("5- File lists\n");
printf("6- Back\n");
printf("7- Exit\n");
printf("Choose your operation? :");
scanf("%d",&select);
}
while((select<=0 )||(select >=8));
system("clear");
switch(select)
{
case 1: printf("You Have select : %d\n",select);
do{
system("clear");
openfile();
printf("You want do this operation again, 1 = No
:");
scanf("%d",&choice);
}
while (choice !=1);
break;
case 2: printf("You Have select : %d\n",select);
do{
system("clear");
filepermission();
printf("You want do this operation again, 1 = No
:");
scanf("%d",&choice);
}
while (choice !=1);
break;
case 3: printf("You Have select : %d\n",select);
do{
system("clear");
readfile();
printf("You want do this operation again, 1 = No
:");

scanf("%d",&choice);
}
while (choice !=1);
break;
case 4: printf("You Have select : %d\n",select);
do{
system("clear");
removefile();
printf("You want do this operation again, 1 = No
:");
scanf("%d",&choice);
}
while (choice !=1);
break;
case 5: printf("You Have select : %d\n",select);
do{
system("clear");
listfile();
printf("You want do this operation again, 1 = No
:");
scanf("%d",&choice);
}
while (choice !=1);
break;
case 6: printf("%d\n",select);
break;
case 7: exit(1);
break;
}
goto operation1;
}

Das könnte Ihnen auch gefallen