//flag:O_RODNLY,O_WRONLY,O_RDWR
//路径:
绝对路径:以/开始的路径称之为绝对路径
eg: /usr/include
相对路径:以“./”开始的路径称之为相对路径,“./”可以不写
./../day01--> ../day01
//使用flag指定的方式打开指定路径的文件
//若文件打开成功,返回该文件一个新的文件描述符
//文件描述符用于文件操作
//若文件失败,返回-1
//int open(const char *pathname, int flag);
===================================================================
把结构体写在文件中
#include <errno.h> //errno
#include <string.h> //strerror()
#include <unistd.h> //close()
/*open(), creat()*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
typedef struct Student
{
char m_Name[20];
int m_Num;
char m_Sex[20];
double m_Grade;
}Student;
int myOpen(const char *pathname)
{
int fd = -1;
if (NULL != pathname)
{
fd = open(pathname, O_WRONLY | O_CREAT
, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (-1 == fd)
{
printf("open error: %s\\n", strerror(errno));
}
}
return fd;
}
int main()
{
int fd = -1;
fd = myOpen("stu.info");
if(-1!=fd)
{
Student stu = {"zhansan",110,"girl",80.55};
int ret = write(fd, &stu, sizeof(Student));
if (-1 == ret)
{
printf("write error: %s\\n", strerror(errno));
}
else
{
printf("write %d bytes to file\\n", ret);
}
}
}
===================================================================
把文件读出来
#include <stdio.h>
#include <unistd.h> //write()
#include <errno.h> //errno
#include <string.h> //strerror()
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define PER_WRITE_BYTES 4096
typedef struct Student
{
char m_Name[20];
int m_Num;
char m_Sex[20];
double m_Grade;
}Student;
int myOpen(const char *pathname)
{
int fd = -1;
if (NULL != pathname)
{
fd = open(pathname, O_RDONLY | O_CREAT
, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (-1 == fd)
{
printf("open error: %s\\n", strerror(errno));
}
}
return fd;
}
int main()
{
int fd = -1;
fd = myOpen("stu.info");
if (-1 != fd)
{
Student stu;
memset(&stu,'\\0',sizeof(Student));
int ret = -1;
ret = read(fd, &stu, sizeof(Student));
if (-1 != ret)
{
printf("id:%d,name:%s,sex:%s,score:%.2lf\\n", stu.m_Num,stu.m_Name,stu.m_Sex,stu.m_Grade);
}
else
{
printf("read error:%s\\n", strerror(errno));
}
close(fd);
}
return 0;
}
===================================================================
写入大量数据,循环写入
#include <stdio.h>
#include <unistd.h> //write()
#include <errno.h> //errno
#include <string.h> //strerror()
/*open()*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define PER_WRITE_BYTES 4096
int myOpen(const char *pathname)
{
int fd = -1;
if (NULL != pathname)
{
fd = open(pathname, O_WRONLY | O_CREAT | O_TRUNC
, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (-1 == fd)
{
printf("open error: %s\\n", strerror(errno));
}
}
return fd;
}
int myWrite(int fd, char *pData, int iTotalSize)
{
//对形参的值进行有效性检查
if (-1 != fd && NULL != pData && iTotalSize > 0)
{
int i = 0;
//若数据量比较大是,write可能一次性写不玩
//这种情况下,必须分多次循环的去写
//剩余要写的字节数
int iLeft = iTotalSize;
//已写入的字节数
int iWirted = 0;
//写数据时的返回值,出错返回-1,
//成功返回实际写入的字节数
int ret = -1;
//如果剩余的数据量大于等于PER_WRITE_BYTES
//则指定该次写入文件的数据量为PER_WRITE_BYTES
//否则指定为剩余的数据量:iLeft
if (iLeft >= PER_WRITE_BYTES)
{
ret = write(fd, pData, PER_WRITE_BYTES);
}
else
{
ret = write(fd, pData, iLeft);
}
if (-1 == ret)
{
printf("write error: %s\\n", strerror(errno));
}
else
{
printf("%d, write %d bytes to file\\n", ++i, ret);
//剩余数据量减去实际写入的数据量
//得到新的剩余数据量
iLeft -= ret;
//已写的数据量加上实际写入的数据量
//得到新的已写数据量
iWirted += ret;
//如果上次写入没有出错并且还有数据没写完
//则循环接着写
while (ret && iLeft)
{
if (iLeft >= PER_WRITE_BYTES)
{
//指针往后偏移到未写的数据位置
//从该位置开始将数据写入文件
ret = write(fd, pData+iWirted
, PER_WRITE_BYTES);
}
else
{
ret = write(fd, pData+iWirted
, iLeft);
}
if (-1 != ret)
{
iLeft -= ret;
iWirted += ret;
printf("%d, write %d bytes to file\\n"
, ++i, ret);
}
else
{
printf("write error: %s\\n", strerror(errno));
}
}
}
}
}
int main()
{
int fd = -1;
fd = myOpen("test.data");
if (-1 != fd)
{
char caBuf[4096789] = {'A'};
myWrite(fd, caBuf, sizeof(caBuf));
close(fd);
}
return 0;
}