文件I/O

来源:互联网 发布:centos安装oracle11g 编辑:程序博客网 时间:2024/05/25 18:09

文件I/O


一、主要函数:
open(),creat(),read(),write(),close(),fcntl(),lseek()
二、open,creat,read,write,close等函数的操作与文件描述符fd有关。

1
、对于open函数而言,仅当创建新文件时才使用第三个参数,由open返回的文件描述符一定是最小的未用描述符数值,常用的常量有O_CREAT,O_TRUNC,O_NONBLOCK,常用open函数代替creat
2
lseek成功执行,返回新的文件偏移量(相对于文件头0所偏移的量);比较lseek的返回值时,不要测试它是否小于0,而要测试它是否等于
-1.
3
read,经常用返回0来判断是否已达结尾。关于read:若在到达文件尾端之前还有30个字节,而要求读100个字节,则read返回30,下一次再调用read时,它将返回0(文件尾端)

4
lseek
    write(fd, "hello", 5);
    lseek(fd, offset, SEEK_END);
    write(fd, "world", 5);

三、fcntl可以改变已打开文件的性质

F_SETFL,F_GETFL,O_ASYNC,O_NONBLOCK
ret = fcntl(fd, F_GETFL);

fcntl(fd, F_SETFL, ret | O_NONBLOCK);
实例:
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main(void)
{
 int fd;
 int n;
 char buf[20];
 int ret;

 fd=open("file1.txt",O_RDWR|O_TRUNC|O_CREAT,0777);//
创建文件时别忘了加mode参数
 if(fd==-1)
 {
 perror("open");
 exit(1);
 }
 // ret=fcntl(0,F_GETFL );//
取得文件信息
 // fcntl(0,F_SETFL,ret|O_NONBLOCK);//fcntl
可以改变已打开文件的性质

 while((n=read(0,buf,20))>0)//read
阻塞,程序不会退出
 if(write(fd,buf,n)!=n)//
读多少个就写多少个
 perror("write");
 exit(0);
}

四、例程
1

/*open.c*/

 

#include <stdio.h>

 

#include <stdlib.h>

 

#include <fcntl.h>

 

int main(int argc, char **argv)

 

{

 

    if (argc != 2) {

 

        fprintf(stderr, "Usags.../n");

 

        return -1;

 

    }

 

 

    close(0);

 

    int fd = open(argv[1], O_RDWR);

 

    if (fd == -1) {

 

        perror("read");

 

        exit(1);

 

    } else {

 

        printf("fd=%d/n", fd);

 

    }

 

 

    close(fd);

 

    return 0;

 

}

2

/*read.c*/

 

#include <stdio.h>

 

#include <stdlib.h>

 

#include <fcntl.h>

 

#include <unistd.h>

 

#include <string.h>


 

int main(void)

 

{

 

    char buf[10];

 

    int ret;

 

    while(1)

 

    {

 

//        bzero(buf,0);

 

        ret=read(0,buf,9);

 

        if(ret==-1)

 

        {

 

            perror("read");

 

            exit(1);

 

        }

 

        buf[ret]='/0';//这一句不能省

 

        printf("%s/n",buf);//如果省去了上一句,那么打印到终端时会出现意想不到的符号

 

    }

 

    exit(0);

 

}

3

/*creat.c*/

 

#include <stdio.h>

 

#include <stdlib.h>

 

#include <fcntl.h>

 

int main(int argc, char **argv)

 

{

 

    if (argc != 2) {

 

        fprintf(stderr, "Usags.../n");

 

        return -1;

 

    }

 

 

    int fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,

 

            0766);

 

    if (fd == -1) {

 

        perror("create");

 

        exit(1);

 

    } else {

 

        printf("fd=%d/n", fd);

 

    }

 

 

    close(fd);

 

    return 0;

 

}

 

/*open代替creat*/

 

4

/*read_write.c*/

 

#include <stdio.h>

 

#include <stdlib.h>

 

#include <fcntl.h>

 

#include <unistd.h>

 

 

int main(void)

 

{

 

    int fd, ret;

 

    char buf[100];


 

    fd = open("/etc/passwd", O_RDONLY);

 

    if (fd == -1) {

 

        perror("open");

 

        exit(1);

 

    }


 

    while (1) {

 

        ret = read(fd, buf, 100);

 

        if (ret == 0)

 

            break;

 

        write(1, buf, ret);//读多少就写多少

 

    }


 

    close(fd);

 

    exit(0);

 

}

5

/*lseek.c*/

 

#include <stdio.h>

 

#include <fcntl.h>

 

#include <stdlib.h>

 

int main(void)

 

{

 

    int fd=open("/etc/passwd-",O_RDONLY);

 

    if(fd==-1)

 

    {

 

        perror("open");

 

        exit(1);

 

    }

 

    int ret;

 

    ret=lseek(fd,5,SEEK_SET);

 

    printf("ret1=%d/n",ret);

 

    ret=lseek(fd,10,SEEK_CUR);//lseek返回相对于文头0所偏移的量

 

    printf("ret2=%d/n",ret);

 

    exit(0);

 

}

6

/*hole.c*/

 

#include <stdio.h>

 

#include <stdlib.h>

 

#include <fcntl.h>

 

#include <unistd.h>

 

int main(int argc, char **argv)

 

{

 

    if (argc != 2) {

 

        exit(1);

 

    }

 

    int fd;

 

    off_t offset = 0x1 << 20;

 

    fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, 0666);

 

    if (fd == -1) {

 

        perror("open");

 

        exit(1);

 

    }

 

    write(fd, "hello", 5);

 

    lseek(fd, offset, SEEK_END);

 

    write(fd, "world", 5);

 

    close(fd);

 

    exit(0);

 

}

 

7

/*fcntl.c*/

 

#include <stdio.h>

 

#include <stdlib.h>

 

#include <fcntl.h>

 

int main(void)

 

{

 

    char buf[32];

 

    int ret;

 

    ret = fcntl(0, F_GETFL);//

 

//  fcntl(0, F_SETFL, ret | O_NONBLOCK);

 

    while (1) {

 

        printf("I am waitting/n");

 

        ret = read(0, buf, 31);

 

        printf("read success!/n");

 

        buf[ret] = '/0';

 

        printf("read: %s", buf);

 

    }

 

    exit(0);

 

}

8、

/*write会覆盖:本例程,xxx覆盖hai*/
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>


int main(void)
{
    int fd=open("testwritecover",O_RDWR | O_CREAT ,0644);
    write(fd,"luoyuhai8702",12);
    lseek(fd,-7,SEEK_CUR);
    write(fd,"xxx",3);
    close(fd);
    return 0;
}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 高院收到申诉材料怎么办? 辞职出国留学后社保怎么办 高三数学40来分怎么办 论文查重表格内容重复怎么办 榕树叶子掉光了怎么办 2017江苏高考2c怎么办 高考刚过三本线怎么办 江苏高考选修有d怎么办 西安地铁卡丢了怎么办 酒后头痛怎么办快速缓解疼痛 孕妇感冒头痛怎么办快速缓解疼痛 投稿后发现文章有错误怎么办 如有一方不同意离婚怎么办 孩子上课时注意力不集中怎么办 幼儿上课时注意力不集中怎么办 孩子读初中不爱读书怎么办 老师需要刺激孩子家长怎么办 错觉视界第11关怎么办 爱逃学的学生老师怎么办 论文投稿发现有错误怎么办 孩子上初中数学物理不好怎么办 火山小视频误踢怎么办 铁棍山药弄到手很痒怎么办 山药皮过敏很痒怎么办 貔貅被家人摸了怎么办? 摸了山药很痒怎么办 山药搞得皮肤痒怎么办 手上弄了山药痒怎么办 老板就是个富二代怎么办 中考准考证密码忘了怎么办 准考证号和密码忘了怎么办 安运继续教育考试不及格怎么办 微信的视频打不开怎么办 公众号被取消了怎么办 合同上不写工资怎么办 已经上班了想考个大专文凭怎么办 微信小程序违规暂停服务怎么办 程序锁密码忘了怎么办 忘了应用锁密码怎么办 毕业后发现论文有错误怎么办 柯丽尔打胶片时胶片卡住了怎么办