#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#define FILENAME "/home/test.txt"
#define FLAGS O_WRONLY | O_CREAT | O_TRUNC
#define MODE 0666
int main(void)
{
char buf1[ ]={"abcdefghij"};
char buf2[ ]={"1234567890"};
int fd;
int count;
const char *pathname=FILENAME;
if ((fd=open(pathname, FLAGS, MODE)==-1))
{
printf("error, open file failed!\n");
exit(1);
}
count=strlen(buf1);
if (write(fd, buf1, count)!=count)
{
printf("error, write file failed!\n");
exit(1);
}
if (lseek(fd, 50, SEEK_SET)==-1)
{
printf("error, lseek failed!\n");
exit(1);
}
count=strlen(buf2);
if (write(fd, buf2, count) != count)
{
printf("error, write file failed!\n");
exit(1);
}
return 0;
}
/*
gcc -o hole hole.c
./hole
ls /home/test.txt
od -c /home/test.txt
*/
求指点:为什么/home/test.txt文件总是为0字节,内容并没有写到文件中
扬帆大鱼
智慧大石
相关分类