#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <unistd.h>
// off_t lseek(int fd, off_t offset, int whence);
// ssize_t read(int fd, void *buf, size_t count);
int main()
{
int fd;
char *buf="jianglongjie henshuai";
fd=open("./file1\n",O_RDWR);
if(fd==-1){
printf("open file failed\n");
fd=open("./file1",O_RDWR|O_CREAT,0600);
if(fd>1){
printf("create successed\n");
}
}
printf("open success fd = %d\n",fd);
int n_write = write(fd,buf,strlen(buf));
if(n_write!=-1){
printf("write %d byte to file\n",n_write);
}
char *readBuf;
readBuf =(char *)malloc (sizeof(char)*n_write+1);
lseek(fd,-21,SEEK_CUR);
int n_read = read(fd,readBuf,100);
printf("read %d ,context: %s\n",n_read,readBuf);
close(fd);
return 0;
}
~
~
~
linux读写文件和光标操作
最新推荐文章于 2022-09-08 11:25:26 发布