给file1文件写入,读取整型数操作
代码演示:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
int data=100;
int data1=0;
//打开file1文件
fd=open("file1",O_RDWR);
//写入数字data,计算该文件占用空间大小
write(fd,&data,sizeof(data));
int size=lseek(fd,0,SEEK_END);
//将光标置于文件头,将file1的内容读取到data1中
lseek(fd,0,SEEK_SET);
read(fd,&data1,sizeof(size));
printf("read %d\n",data1);
close(fd);
return 0;
}
运行结果,file1从一个空文件变为了乱码,不影响系统运行,但是人眼看来是乱码
d^@^@^@