常用的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

相关推荐
.道阻且长.4 小时前
2.LeetCode算法习题讲解--双指针--复写零
算法·leetcode·职场和发展
ltl4 小时前
内核追踪:ftrace、kprobe、uprobe、tracepoint 生产实战
linux
·薯条大王4 小时前
经济实惠玩云服务器|一台云服务器多人共用,子账号配置教程
java·linux·运维·服务器·汇编·c++·python
To_OC6 小时前
LC 438 找到所有字母异位词:暴力超时后,我靠滑动窗口一招搞定
javascript·算法·leetcode
小小、码农8 小时前
Linux进程控制:从fork到exec,手写一个简易Shell
linux·运维·服务器
命运之光8 小时前
【C语言完整代码】就诊信息管理系统
java·c语言·开发语言
不会就选b8 小时前
Linux之进程间通信(二)---模拟进程池
linux·运维·服务器
命运之光8 小时前
【C语言完整代码】图书管理系统:图书馆场景下的增删改查实战
c语言·开发语言
Forever Nore9 小时前
学完C语言力扣第一题做不来正常吗
数据结构·算法