发布网友 发布时间:2022-04-23 09:02
共1个回答
热心网友 时间:2023-10-08 23:56
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main() {
int fd = -1;
fd = open("_561804018.dat", O_CREAT | O_TRUNC | O_RDWR, 0666);
if (fd < 0) {
perror("open");
return -1;
}
char buff[];
strcpy(buff, "Hello!I am writing to this file!");
int count = strlen(buff);
if (write(fd, buff, count) < 0) {
perror("write");
return -1;
}
if (lseek(fd, 0, SEEK_SET) < 0) {
perror("lseek");
return -1;
}
if (read(fd, buff, 10) < 0) {
perror("read");
return -1;
}
buff[10] = 0x00;
printf("%s\n", buff);
if (fd > 0) {
close(fd);
fd = -1;
}
return 0;
}