常用的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-其他用户

代码

Delphi 复制代码
 <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};//创建空的字符串writeBuff
        char readBuff[readNum] = {0};//创建空的字符串readBuff
        char *test = "hello world";//在指针字符串test里面存储字符串hello world
        if(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

相关推荐
yaoh.wang7 小时前
力扣(LeetCode) 13: 罗马数字转整数 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·跳槽
AI浩7 小时前
【Labelme数据操作】LabelMe标注批量复制工具 - 完整教程
运维·服务器·前端
T1ssy7 小时前
布隆过滤器:用概率换空间的奇妙数据结构
算法·哈希算法
石像鬼₧魂石7 小时前
如何配置Fail2Ban的Jail?
linux·学习·ubuntu
sunxunyong7 小时前
doris运维命令
java·运维·数据库
Guheyunyi7 小时前
智慧消防管理系统如何重塑安全未来
大数据·运维·服务器·人工智能·安全
hetao17338378 小时前
2025-12-12~14 hetao1733837的刷题笔记
数据结构·c++·笔记·算法
椰子今天很可爱8 小时前
五种I/O模型与多路转接
linux·c语言·c++
Lueeee.8 小时前
Linux kernel Makefile 语法
linux
程序员zgh8 小时前
C++ 互斥锁、读写锁、原子操作、条件变量
c语言·开发语言·jvm·c++