- 论坛徽章:
- 0
|
我用LINUX系统调用来读写文件,如下面代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
int main(void)
{
int fd;
char psz[] = "Linux is good!";
char buf[20] = {'\0'};
int len;
fd = open("a.txt", O_RDWR|O_CREAT, 0777);
len = write(fd, psz, strlen(psz));
len = read(fd, buf, sizeof(buf));
buf[len] = '\0';
close(fd);
printf("buf: %s\n", buf);
return 0;
}
该程序编译执行后,发现有两个问题
1、创建出来的文件权限不对,并没有777的权限,是
-rwxr-xr-x 1 root root 14 05-10 03:36 a.txt
2、在read会来的buf数据不对,根本就没有读回来数据,len返回值是0
怀疑是文件系统缓存问题,但是系统调用函数中,我没有找到象C函数
中fflush函数的功能
我用C函数来写,测试可以得到正确的结果。
简单的程序,没想到会冒出两个错误,两个问题该如何解决呢?
请高手指点,I am a Linux beginner |
|