欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 新车 > 常用的Linux命令,linux下文件的读、写、打开、关闭append用法

常用的Linux命令,linux下文件的读、写、打开、关闭append用法

2024/10/25 12:23:32 来源:https://blog.csdn.net/weixin_43399456/article/details/139425830  浏览:    关键词:常用的Linux命令,linux下文件的读、写、打开、关闭append用法

 

 


vim demoq.c打开写的.c文件

内容为

按a可以编辑页面代码。按ESC退出编辑然后按shift+:wq保存文件并退出

Linux 系统中采用三位十进制数表示权限,如0755, 0644.7 1+2+4(可读、可写、可执行)
5 1+4(可读、不可写、可执行)

5 1+4(可读、不可写、可执行)ABCD
A- 0, 表示十进制
B-用户
C-组用户
D-其他用户

代码

 <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>//off_t lseek(int fd, off_t offset, int whence);l_seek函数
//int open(const char *pathname, int flags);open函数
//int open(const char *pathname, int flags, mode_t mode);open函数,末尾的mote可以为0755rdx
//ssize_t write(int fd, const void *buf, size_t count);write函数
//ssize_t read(int fd, void *buf, size_t count);read函数
#define filename "mm"//将“mm”定义为filename
#define writeNum 128//将128定义为writeNum
#define readNum 12//将12定义为readNum
int main()
{int offset;//定义offset整型int fd;//定义fd整型char writeBuff[writeNum] = {0};//创建空的字符串writeBuffchar readBuff[readNum] = {0};//创建空的字符串readBuffchar *test = "hello world";//在指针字符串test里面存储字符串hello worldif(writeNum<(strlen(test)+1))//if函数为了判断写的空间是否小于字符串的长度加1,1为字符串末尾的标识符,占一个字符长度,如果写的空间小于,则报错,不能写{printf("error:writeBuff less than test\n");return -1;}strcpy(writeBuff,test);//将指针变量test里面的字符串复制给writeBuff
//int open(const char *pathname, int flags);open函数fd = open("mm",O_APPEND|O_RDWR|O_CREAT,0755);//open函数,第一个为文件命名,第二个为文件格式:O_APPEND添加,O_RDWR可读可写,O_CREAT创建文件,第三个为追加模式0755,用户,组用户和其他用户可读可写可执行的权限if(fd == -1)//如果fd == -1则表明打开文件失败,返回-1{printf("open failed!");perror("why");return -1;}printf("open successed!");//否则,打开文件成功
//off_t lseek(int fd, off_t offset, int whence);lseek函数,偏移量off_set
//      offset = lseek(fd,1,SEEK_CUR);//,表示从fd文件当前光标位置偏移一个字节printf("offset is %d\n",offset);
//ssize_t write(int fd, const void *buf, size_t count);write函数write(fd,&writeBuff[0],11);//写的对象文件fd,写的首地址&writeBuff[0],写入字节的大小
//      read(fd,&readBuff,1);//读的对象为文件fd,读的首地址&readBuff,读入字节的大小
//      printf("%s \n",readBuff);//输出读的存放在readBuff里的字符串close(fd);//关闭文件return 0;//返回0}

每编译一次,cat一次,会在末尾append一次 hello world 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com