26_linux 文件编程
linux 文件编程
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
int main(int argc, char const *argv[])
{
int fd, len;
char *buf = "Hello World\n", Out[32];
fd = open("a.txt", O_CREAT|O_TRUNC|O_RDWR,0600);
printf("open file: a.txt fd = %d\n", fd);
len = strlen(buf);
int size = write(fd, buf, len);
close(fd);
fd = open("a.txt", O_RDWR, 0600);
lseek(fd, 0, SEEK_SET);
size = read(fd, Out, 12);
printf("size = %d\nread from file:\n%s\n", size, Out);
close(fd);
return 0;
}