C查看当前文件的权限
#include <unistd.h>
#include <stdio.h>
int main(int argc,char* argv[])
{
char *my[3] = {0};
my[0]="ls";
my[1] = "-l";
execv("/bin/ls",my);
return 0;
}
C打开文件与关闭文件
参考:https://blog.csdn.net/annjeff/article/details/107086983?utm_medium=distribute.pc_relevant.none-task-blog-title-5&spm=1001.2101.3001.4242
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> // fclose
#include <stdlib.h> //exit
#include <stdio.h> //perror
int main()
{
int fd;
// 光标在 open 身上,2 shift + k 即可跳至帮助文档
// 打开文件,若该文件不存在则创建新文件
fd = open("annNote", O_RDWR | O_CREAT, 0777);
if(fd == -1){
perror("create file");
exit(1);
}
// 关闭文件
int ret = close(fd);
if( ret == -1){
perror("close file");
exit(1);
}
return 0;
}
Linux下用C语言使用write函数实现将键盘输入的内容写入文件
参考:https://blog.csdn.net/zxy131072/article/details/108238257
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define N 128
int main(int argc, const char *argv[])
{
int fd;
char buf[N] = { 0 };
memset(buf, 0, sizeof(buf));
if ((fd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
{
perror("open");
return -1;
}
while (fgets(buf, 20, stdin) != NULL)
{
if (strcmp(buf, "quit\n") == 0)
break;
write(fd, buf, strlen(buf));
}
close(fd);
return 0;
}
Linux中C代码设置文件的权限
https://baike.baidu.com/item/MKDIR/9500968?fr=aladdin
实验需求
代码如下,创建代码为demo.c
#include<stdio.h> //标准输入输出函数
#include<stdlib.h> //C、C++语言的最常用的系统函数
#include<sys/types.h>
#include<sys/stat.h>
#include<syslog.h>
#include<string.h>
//补充缺失的头文件
#include<unistd.h>
#include <fcntl.h>
#include <unistd.h> // fclose
#include <stdlib.h> //exit
#include <stdio.h> //perror
#define MAX 128
int chmd()
{
int c;
mode_t mode=S_IWUSR;
printf("0: 0700\n1: 0400\n2: 0200\n3: 0100\n");
printf("Please input your choice of change the mode of the file :");
scanf("%d", &c);
switch(c)
{
//参考:https://baike.baidu.com/item/MKDIR/9500968?fr=aladdin
case 0:chmod("file1", S_IRWXU); break; //0700权限
// Mode flag: Read, write, execute by user.
//依据上面数字的提示定义其他case中文件权限的情况。
//补充代码:case1
case 1: chmod("file1", S_IRUSR); break;//0400权限
//补充代码:case2
case 2: chmod("file1", S_IWUSR); break;//0200权限
//补充代码:case3
case 3: chmod("file1", S_IXUSR); break;//0100权限
default:printf("You have a wrong choice! \n");
}
return 0;
}
int main()
{
int fd;
int num;
int choice;
char buffer[MAX];
struct stat st;
int n;
char buf[1024];
char* path="/bin/ls";
char* argv[4]={"ls","-l","file1",NULL};
while(1)
{
printf("*************************\n");
printf("0. 退出\n");
printf("1. 创建新文件\n");
printf("2. 写文件\n");
printf("3. 读文件\n");
printf("4. 修改文件权限\n");
printf("5. 查看当前文件的权限并退出\n");
printf("*************************\n");
printf("Please input your choice(0-6):");
scanf("%d", &choice);
switch(choice)
{
case 0: close(fd);
exit(0);
case 1:
fd=open("file1",O_RDWR|O_TRUNC|O_CREAT,0750);
/*
O_RDWR : file open mode: Read/Write
O_TRUNC : file open mode: Truncate file to length 0
O_CREAT : file open mode: Create if file does not yet exist.
0750: file access permission bits -rwxr-x---当前用户rwx;同组用户r-x;其他用户无权限
*/
if(fd==-1)
printf("File Create Failed!\n");
else
printf("fd=%d\n",fd);
break;
case 2:
//补充代码:用read与write函数,从键盘里面读取信息,写到filel里面
printf("输入需要写入到file1中的内容\n");
fgets(buf, 20, stdin);//读掉上一行的行末符号
fgets(buf, 20, stdin);
fd = open("file1", O_RDWR | O_CREAT, 0777);
write(fd, buf, strlen(buf));
printf("写入成功\n");
break;
case 3:
fd = open("file1", O_RDWR | O_CREAT, 0777);
printf("读取file1成功\n");
while((n = read(fd, buf, 1024)) > 0 ){
printf("%s\n",buf);
}
break;
//补充代码:用read与write函数,把file1文件的内容在屏幕上输出
case 4:
chmd();
printf("Change mode success!\n");
break;
case 5:
printf("输出file1文件的权限、拥有者、拥有组、文件名、大小等\n");
execv(path, argv); //-ls -l 显示文件完整的信息,包括权限,拥有者,拥有组,文件名,大小等
break;
default:
printf("You have a wrong choice!\n");
}
}
return 0;
}
|