- 论坛徽章:
- 1
|
本帖最后由 openspace 于 2012-07-16 16:10 编辑
好长时间没写过了,需要写个小程序,测试一个功能,但是这个小程序总是报错- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <string.h>
- #include <stdlib.h>
- #include <errno.h>
- static int flag = O_RDWR | O_CREAT | O_TRUNC;
- static int mode = S_IRWXU | S_IRWXG;
- static int buf_size = 4096;
- static int file_size = 4096 * 4096;
- /*
- * populate the target file with character '='.
- */
- static int populate_file()
- {
- char buf[buf_size+1];
- int targetfd;
- int size = 0, tmpsize = 0, iter = 0;
- targetfd = open("testfile", flag, mode);
- if (targetfd < 0) {
- perror("open target file failed");
- exit(errno);
- }
- memset(buf, '=', buf_size);
- buf[buf_size] = '\0';
- while (size < file_size) {
- tmpsize = write(targetfd, buf, buf_size);
- printf("iter %d: written %d\n", iter, tmpsize);
- if (tmpsize < 0) {
- perror("write target file failed");
- exit(errno);
- }
- iter++;
- size += tmpsize;
- if (file_size - size < buf_size);
- buf_size = file_size - size;
- }
- return targetfd;
- }
- int main()
- {
- int targetfd;
- targetfd = populate_file();
- close(targetfd);
- return 0;
- }
复制代码 输出如下:
iter 0: written 4096
iter 1: written -1
write target file failed: Bad address
|
|