您的位置:首页 > 运维架构 > Linux

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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: