Linux文件的读取与及光标的移动
read()
lseek()
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main()
{
int fd;
char *buf = "chenlechen hen shuai!";
fd = open("./file2",O_RDWR|O_CREAT,0600);
if(fd != -1){
printf("open file2 success\n");
}
// printf("open success :fd = %d\n",fd);
// write(fd,buf,strlen(buf));
int n_write = write(fd,buf,strlen(buf));
if(n_write!=-1){
printf("write %d byte to file2\n",n_write);
}
int n_read = (fd,buf,n_write);
lseek(fd,0,SEEK_SET);
printf("read %d,context:%s",n_read,buf);
return 0;
}