#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
char *buf = "longhaiyanghenshuai";
fd = open("./file1",O_RDWR);
if(fd = -1){
printf("open failed\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd > 0 ){
printf("creat successful\n");
}
}
int n_write = write(fd,buf,strlen(buf));
if(n_write != -1){
printf("write successful\n");
}
close(fd);//对文件写操作 写完光标在内容尾部,关闭文件重新打开让光标到内容头部
fd = open("./file1",O_RDWR);
char *readbuf = (char *)malloc(sizeof(char)*n_write+1);
int n_read = read(fd,readbuf,n_write);
printf("read = %d,context:%s\n",n_read,readbuf);
close(fd);
}```
Linux文件编程demo4文件读取操作.c
最新推荐文章于 2025-03-24 10:19:46 发布
291

被折叠的 条评论
为什么被折叠?



