系统网络


//flag:O_RODNLY,O_WRONLY,O_RDWR
//路径:
    绝对路径:以/开始的路径称之为绝对路径
                   eg:   /usr/include
    相对路径:以“./”开始的路径称之为相对路径,“./”可以不写
                      ./../day01--> ../day01
//使用flag指定的方式打开指定路径的文件
//若文件打开成功,返回该文件一个新的文件描述符
//文件描述符用于文件操作
//若文件失败,返回-1
//int open(const char *pathname, int flag);
1.PNG

===================================================================

把结构体写在文件中
#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;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 系统网络50问1、标准文件IO与文件IO的区别?答:标准IO:标准I/O是ANSI C建立的一个标准I/O模型,是...
    萌面大叔2阅读 431评论 0 0
  • 系统网络50问1、标准文件IO与文件IO的区别?答:标准IO:标准I/O是ANSI C建立的一个标准I/O模型,是...
    sky_yang阅读 514评论 0 2
  • 网络应用集成了我们已经学到的很多概念:进程、信号、字节顺序、存储器映射、动态分配等,同时客服端-服务器模型是一个新...
    唐鱼的学习探索阅读 1,330评论 0 2
  • 由于针对工业控制系统的网络攻击持续增多,如何确保工业控制系统的网络安全成为各国越来越重视的课题。通过借鉴国际上相关...
    简说666阅读 2,304评论 0 1
  • 此时是2016年八月十八凌晨两点三十分,秋天带来的凉风使我失眠,也让我得以思考 秋天一直都是我喜欢的季节,或许是因...
    酒心橙子阅读 120评论 0 0