秀杰空间

秀杰笔记
做些有意义的事情
  1. 首页
  2. Linux/Unix C/C++
  3. 正文

UNIX环境高级编程学习之第三章文件IO-文件读操作

2016年7月19日 2726点热度 0人点赞 0条评论

UNIX环境高级编程学习之第三章文件IO-文件读操作
[code lang="cpp"]
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int err_sys(const char * str)
{
printf("%s/n", str);
exit(-1);
return 0;
}
int main(int argc, char *argv[])
{
int fd;
fd = open("./a.txt", O_RDONLY);
if (-1 == fd)
{
err_sys(strerror(errno));
}
int i;
double d;
float f;
char c;
char str[100] = { '/0' };
int ret = read(fd, &i, sizeof(i));
if (ret != 4)
{
err_sys("read error!");
}
ret = read(fd, &d, sizeof(double));
if (ret != sizeof(double))
{
err_sys("read error! ");
}
ret = read(fd, &f, sizeof(f));
if (ret != sizeof(f))
{
err_sys("read error!");
}
ret = read(fd, &c, sizeof(c));
if (ret != sizeof(c))
{
err_sys("read error!");
}
ret = read(fd, str, sizeof(str));
if (ret != sizeof(str))
{
err_sys("read error!");
}
close(fd);
printf("i=%d, d=%lf, f=%f, c=%c, str=%s/n", i, d, f, c, str);
}

[/code]

标签: UNIX环境高级编程
最后更新:2016年7月19日

秀杰

做些有意义的事情

点赞
< 上一篇
下一篇 >

文章评论

您需要 登录 之后才可以评论

COPYRIGHT © 2023 个人笔记. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

京ICP备11019155号-2