当前位置: 首页 > wzjs >正文

企业网站设计意义快速开发平台开发

企业网站设计意义,快速开发平台开发,揭阳网站建设团队,时空网站建设的可行性分析1.文件编程 window如何手动修改文件 比如写一个word文档: Linux呢? 打开/创建文档 -> 编辑文档 -> 保存文档 -> 关闭文档 计算机如何帮助我们自动化完成以上操作 操作系统提供了一系列的API 如Linux系统: 打开 op…

1.文件编程

window如何手动修改文件 比如写一个word文档: Linux呢?

打开/创建文档  ->  编辑文档  ->  保存文档  ->  关闭文档

计算机如何帮助我们自动化完成以上操作

操作系统提供了一系列的API

如Linux系统:

打开       open

读写        write /read

光标定位 lseek    

关闭       close

打开/创建文件

参数说明

Pathname:要打开的文件名(含路径,缺省为当前路径) Flags:     O_RDONLY 只读打开        O_WRONLY 只写打开        O_RDWR 可读可写打开    当我们附带了权限后,打开的文件就只能按照这种权限来操作。    

以上这三个常数中应当只指定一 个。下列常数是可选择的:            

O_CREAT 若文件不存在则创建它。使用此选项时,需要同时说明第三个参数mode,用其说明该新文件的存取许可权限。        O_EXCL 如果同时指定了OCREAT,而文件已经存在,则出错。                 O_APPEND 每次写时都加到文件的尾端。        O_TRUNC 属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0。

Mode:一定是在flags中使用了O_CREAT标志,mode记录待创建的文件的访问权限。

#include <sys/types.h>
       #include <sys/stat.h>
       #include <fcntl.h>
#include <stdio.h>

int main()
{
        //int open(const char *pathname, int flags);
        int fd;
       fd= open ("./file0",O_RDWR);
      return 0;
}

更改为

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

int main()
{
        //int open(const char *pathname, int flags);
        int fd;
       fd= open ("./file0",O_RDWR);
       if(fd == -1){
                 printf("fd = %d\n",fd);
                fd = open("./file0",O_RDWR|O_CREAT,0600);
                if(fd > 0){
                        printf("creat file0 success!\n");
                        printf("fd = %d\n",fd);
                }
       }
      return 0;
}
 

创建文件creat函数

写入文件

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main()
{
        //int open(const char *pathname, int flags);
        int fd;
        char *buf;
       fd= open ("./file0",O_RDWR);
       if(fd == -1){
                 printf("fd = %d\n",fd);
                fd = open("./file0",O_RDWR|O_CREAT,0600);
                if(fd > 0){
                        printf("creat file0 success!\n");
                        printf("fd = %d\n",fd);
                }
       }

       printf("open file0 success!\n");

//ssize_t write(int fd, const void *buf, size_t count);
       write(fd,buf,strlen(buf));

       close(fd);

      return 0;
}
 

读取文件

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main()
{
        //int open(const char *pathname, int flags);
        int fd;
        char *buf = "mmm is my lover";
       fd= open ("./file0",O_RDWR);
       if(fd == -1){
                 printf("fd = %d\n",fd);
                fd = open("./file0",O_RDWR|O_CREAT,0600);
                if(fd > 0){
                        printf("creat file0 success!\n");
                        printf("fd = %d\n",fd);
                }
       }

       printf("open file0 success!\n");

//ssize_t write(int fd, const void *buf, size_t count);
       int n_write = write(fd,buf,strlen(buf));
        if(n_write != -1){
                printf("write success!\n");
        }

        close(fd);
        fd= open ("./file0",O_RDWR);

        char *readBuf;
        readBuf = (char *)malloc(sizeof(char)*n_write+1);
   //   ssize_t read(int fd, void *buf, size_t count);
        int n_read = read(fd, readBuf,n_write);
        if(n_read != -1){
                printf("read success!\n");
                printf("read:%d,text:%s\n",n_read,readBuf);


        }


        close(fd);

      return 0;
}

文件”光标”位置

将文件读写指针相对whence移动offset个字节

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main()
{
        //int open(const char *pathname, int flags);
        int fd;
        char *buf = "mmm is my lover";
       fd= open ("./file0",O_RDWR);
       if(fd == -1){
                 printf("fd = %d\n",fd);
                fd = open("./file0",O_RDWR|O_CREAT,0600);
                if(fd > 0){
                        printf("creat file0 success!\n");
                        printf("fd = %d\n",fd);
                }
       }

       printf("open file0 success!\n");

//ssize_t write(int fd, const void *buf, size_t count);
       int n_write = write(fd,buf,strlen(buf));
        if(n_write != -1){
                printf("write success!\n");
        }

// off_t lseek(int fd, off_t offset, int whence);
        lseek(fd,0,SEEK_SET);

        char *readBuf;
        readBuf = (char *)malloc(sizeof(char)*n_write+1);
   //   ssize_t read(int fd, void *buf, size_t count);
        int n_read = read(fd, readBuf,n_write);
        if(n_read != -1){
                printf("read success!\n");
                printf("read:%d,text:%s\n",n_read,readBuf);


        }


        close(fd);

      return 0;
}
 

关闭文件

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main()
{
        //int open(const char *pathname, int flags);
        int fd;
        char *buf = "mmm is my lover";
       fd= open ("./file0",O_RDWR);
       if(fd == -1){
                 printf("fd = %d\n",fd);
                fd = open("./file0",O_RDWR|O_CREAT,0600);
                if(fd > 0){
                        printf("creat file0 success!\n");
                        printf("fd = %d\n",fd);
                }
       }

       printf("open file0 success!\n");

//ssize_t write(int fd, const void *buf, size_t count);
       int n_write = write(fd,buf,strlen(buf));
        if(n_write != -1){
                printf("write success!\n");
        }

// off_t lseek(int fd, off_t offset, int whence);
        int fileSize = lseek(fd,0,SEEK_END);
        printf("file size is :%d\n",fileSize);

        char *readBuf;
        readBuf = (char *)malloc(sizeof(char)*n_write+1);
   //   ssize_t read(int fd, void *buf, size_t count);
        int n_read = read(fd, readBuf,n_write);
        if(n_read != -1){
                printf("read success!\n");
                printf("read:%d,text:%s\n",n_read,readBuf);


        }


        close(fd);

      return 0;
}
 

文件编程的一般步骤

打开/创建文件  ->  读取文件/写入文件 -> 关闭文件

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char **argv)
{
        int fdSrc;
        char *readbuf;
        int fdDes;

        if(argc != 3){
                printf("Param Error!\n");
                exit(-1);
        }
        fdSrc = open(argv[1],O_RDWR);

        int size = lseek(fdSrc,0,SEEK_END);

        lseek(fdSrc,0,SEEK_SET);

        readbuf = (char*)malloc(sizeof(char)*size + 8);
        read(fdSrc, readbuf, size);

        fdDes = open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);

        write(fdDes,readbuf,strlen(readbuf));

        close(fdSrc);
        close(fdDes);

      return 0;
}
 

文件编程练手1

实现linux cp命令的代码

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char **argv)
{
        int fdSrc;
        char *readbuf;
        int fdDes;

        if(argc != 3){
                printf("Param Error!\n");
                exit(-1);
        }
        fdSrc = open(argv[1],O_RDWR);

        int size = lseek(fdSrc,0,SEEK_END);

        lseek(fdSrc,0,SEEK_SET);

        readbuf = (char*)malloc(sizeof(char)*size + 8);
        read(fdSrc, readbuf, size);

        fdDes = open(argv[2],O_RDWR|O_CREAT,0600);

        write(fdDes,readbuf,strlen(readbuf));

        close(fdSrc);
        close(fdDes);

      return 0;
}


文件编程练手2

配置文件的修改

//配置文件的修改,该demo10更改text.config文件里面LENG=3的值。

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char **argv)
{
        int fdSrc;
        char *readbuf;

        if(argc != 2){
                printf("Param Error!\n");
                exit(-1);
        }
        fdSrc = open(argv[1],O_RDWR);

        int size = lseek(fdSrc,0,SEEK_END);

        lseek(fdSrc,0,SEEK_SET);

        readbuf = (char*)malloc(sizeof(char)*size + 8);
        read(fdSrc, readbuf, size);

        char *p = strstr(readbuf,"LENG=");
        if(p == NULL){
                printf("Not Found!\n");
                exit(-1);
        }
        p += strlen("LENG=");
        *p = '8';

        lseek(fdSrc,0,SEEK_SET);

        write(fdSrc,readbuf,strlen(readbuf));

        close(fdSrc);

      return 0;
}

文件如何记录一个整数


#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main()
{
        int fd = open("./file1",O_RDWR|O_TRUNC);

        int data = 5;
        int data1;

        write(fd,&data,sizeof(int));

        lseek(fd,0,SEEK_SET);

        read(fd,&data1,sizeof(int));

        printf("read : %d\n",data1);


        close(fd);

      return 0;
}

文件如何记录一个结构体

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

struct Test{

        int a;
        char c;
};

int main()
{
        struct Test data = {5, 'a'};
        struct Test data1;

        int fd = open("./file1",O_RDWR|O_TRUNC);


        write(fd,&data,sizeof(struct Test));

        lseek(fd,0,SEEK_SET);

        read(fd,&data1,sizeof(struct Test));

        printf("read : %d,%c\n",data1.a,data1.c);


        close(fd);

      return 0;
}

文件如何记录一个结构体数组

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

struct Test{

        int a;
        char c;
};

int main()
{
        struct Test data[2] ={ {5, 'a'},{100,'b'}};
        struct Test data1[2];

        int fd = open("./file1",O_RDWR|O_TRUNC);


        write(fd,&data,sizeof(struct Test)*2);

        lseek(fd,0,SEEK_SET);

        read(fd,&data1,sizeof(struct Test)*2);

        printf("read : %d,%c\n",data1[0].a,data1[0].c);
        printf("read : %d,%c\n",data1[1].a,data1[1].c);


        close(fd);

      return 0;
}


文章转载自:

http://5ldQEsoM.fkgct.cn
http://lfrruJQg.fkgct.cn
http://O3gMa9VY.fkgct.cn
http://G4o0g6Yw.fkgct.cn
http://t2mF0IWl.fkgct.cn
http://n7XP9W4R.fkgct.cn
http://XyYFmx38.fkgct.cn
http://YC4CxxKW.fkgct.cn
http://5W6Utc10.fkgct.cn
http://HqFZ2Uwk.fkgct.cn
http://3VQmHRb5.fkgct.cn
http://DH8jxudl.fkgct.cn
http://qCCUapT0.fkgct.cn
http://U1NFxI0J.fkgct.cn
http://bpqCLWzY.fkgct.cn
http://0QXdWgei.fkgct.cn
http://jnXrscO8.fkgct.cn
http://b25TVHO4.fkgct.cn
http://30F2RhcY.fkgct.cn
http://PQTul5Jb.fkgct.cn
http://DAv7Hu53.fkgct.cn
http://Y1CmvBp7.fkgct.cn
http://VecODw1e.fkgct.cn
http://xLKu4cMB.fkgct.cn
http://vMkKOaRm.fkgct.cn
http://0xB0sBK2.fkgct.cn
http://hySbmlcW.fkgct.cn
http://fwndECyt.fkgct.cn
http://TCx3S21O.fkgct.cn
http://GJtstuJf.fkgct.cn
http://www.dtcms.com/wzjs/742381.html

相关文章:

  • 北京驾校网站建设如何提交网站给百度
  • 申请网站长沙seo培训
  • 北京网站备案查询优秀企业网站建设
  • 重庆城乡建设网站首页wordpress安全漏洞
  • 建设银行官网站下载地址山东济宁刚刚出大事
  • 免费建站的平台免费咨询律师的电话
  • 国内免费自建网站东莞寮步
  • 宁波怎么建网站模板wordpress百度云链接地址
  • 皇家梅陇公馆网站建设品牌型网站的设计
  • 一个营业执照可以做几个网站wordpress偽靜態
  • 网站建设对网络营销有哪些影响有没有做生物科技相关的网站
  • python搭建网站鹤壁网站建设公司
  • 淘宝网站开发实训报告目录现在网站开发技术有哪些
  • 做网站建设公司crm在线的提升服务河北省城乡和建设厅网站
  • 学院网站建设项目的成本计划书济南网站开发xywlcn
  • 保山市网站建设手游折扣平台app哪个好
  • 桐柏微网站建设专门查大学的网站
  • 网上挣钱正规渠道淘宝网站可以做seo吗
  • 专业建设家电维修网站公司做网站是否用数据库
  • 金华商城网站制作wordpress文章保存
  • 广东省建设监理协会网站整站优化案例
  • 中国建设网站轨道自检验收报告表wordpress页面模板链接
  • 丰台网站制作浩森宇特dedecms网站别名
  • 济南网站建设公司排名网站建设建站培训
  • 织梦怎么更新网站html编程网站scratch在线使用
  • 网站建设出错1004大一做家教的网站
  • 福州企业网站php做的商城网站必备功能
  • 做免费导航网站搜索引擎营销的原理
  • crm管理系统在线使用如何做谷歌优化
  • 网上商城排行优化网站的公司