Linux 文件读写
2016-07-14 00:46
417 查看
1、最基础的读写函数
open() read() write() 需要的头文件: #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h>
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <iostream> #include <string.h> #include <stdio.h> using namespace std; int main() { /*write()函数*/ int fd = open("/data/joyyzhang/mypro/a", O_CREAT|O_WRONLY|O_TRUNC); const char *buff = "This is an io test !!!\n"; ssize_t nr; nr = write(fd,buff,strlen(buff)); close(fd); if(nr == -1) { /*error*/ } /*read()函数*/ fd = open("/data/joyyzhang/mypro/test.cpp", O_RDONLY); if(fd == -1) { /*error*/ } int len=100; char buf[len]; while(len != 0 && (nr = read(fd,buf,len)) != 0) { if(nr == -1) { /*error*/ } printf("%s",buf); memset(buf,0,sizeof(buf)); } close(fd); }
相关文章推荐
- Linux 文件读写
- Linux 文件读写
- Linux 文件读写
- Linux基础篇——信号(2)
- Centos7安装杀毒软件ClamAV
- Linux学习笔记--常用目录的作用
- linux $变量含义($0,$1,$2,$#,$@) ----linux 基础
- GNOME 2 粉丝喜欢 Mate Linux 桌面的什么?
- Linux下如何增量增量传输MySQL binlog日志
- Linux 系统结构详解
- 在 Linux 下使用 scp 命令
- 靠自己。linux manul手册入门
- 微软Skype Linux客户端全新发布
- 在Centos上安装gitlab
- Centos6.4编译安装MySQL 5.6
- linux系统 I/O重定向、管道及grep用法详解
- Linux vmstat命令实战详解
- 红帽推出业界最完整的Linux容器解决方案
- linux系统中df 与du 大小显示不一致
- Linux命令(基础)