- 论坛徽章:
- 0
|
- #include <stdio.h
- #include <sys/time.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <malloc.h>
- int main()
- {
- char *buf = NULL;
- int fd = -1, ret = -10;
- buf = valloc(4100);
- printf("buf address = %p\n", buf);
-
- buf = "12345\n";
- printf("the buf is:%s\n",buf);
-
- fd = open("/home/zhangqi/deadfish.txt", O_RDWR | O_DIRECT | O_CREAT);
-
- if (fd<0){
- printf("Cannot open file\n");
- exit(1);
- }
-
- printf("fd = %d\n",fd);
-
- ret = write(fd, buf, 4096);
- if (ret < 4096)
- printf("write error!\n");
-
- printf("after write...\nret = %d\n",ret);
-
- free(buf);
- close(fd);
- return 0;
- }
复制代码 运行的结果是:- buf address = 0x804b000
- the buf is:12345
- fd = 3
- write error!
- after write...
- ret = -1
- Segmentation fault
复制代码 大伙帮忙看看。
要写的文件是生成了,但是内容没有写进去。。。 |
|