Chinaunix首页 | 论坛 | 博客
  • 博客访问: 287818
  • 博文数量: 134
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 118
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-01 14:02
文章分类

全部博文(134)

文章存档

2015年(2)

2014年(4)

2013年(128)

分类: C/C++

2013-10-22 19:23:04

/*1.制作文件1.txt -- 10.txt
1.txt  1 2 3 4 5 6 7 8 9 10
2.txt  11 12 13 14 15 16 17 18 19 20
...
10.txt 91 92 93 94 95 96 97 98 99 100
sprintf(buf,"%d.txt",2);

/*2.依次合并1.txt--10.txt 到 文件data.txt
合并操作完成后,data.txt文件的内容
1 2 3 4 5 6 7 8 9 10
......
91 92 93 94 95 96 97 98 99 100*/

#include
#include
#include
#include
#include

int main(void)
{
    int n=1;
    int i=0,j=0;
    int m=0;
    char buf[100]={};

    for(j=1;j<=10;j++)
    {
        sprintf(buf,"%d.txt",j);
        int fd=open(buf,O_RDWR|O_CREAT|O_TRUNC,0755);
        if(fd<0)
        {
            perror("open fail\n");
            exit(1);
        }
        for(i=0;i<10;i++)
        {
            m=sprintf(buf,"%3d ",n);
            write(fd,buf,m);
            n=n+1;
        }

        write(fd,"\n",1);

        close(fd);
    }


    int fd=open("data.txt",O_WRONLY|O_TRUNC|O_CREAT,0755);
    if(fd<0)
    {
        perror("open fail\n");
        exit(1);
    }

    for(i=0;i<10;i++)
    {
        sprintf(buf,"%d.txt",i+1);
        int fd1=open(buf,O_RDONLY);
        if(fd1<0)
        {
            perror("open fail");
            exit(1);
        }
        while((n=read(fd1,buf,sizeof(buf)))>0)
        {
            write(fd,buf,n);
        }

        close(fd1);
    
    }

    close(fd);
    
    return 0;

}
阅读(2305) | 评论(0) | 转发(0) |
0

上一篇:文件操作库函数

下一篇:mmap 函数 映射

给主人留下些什么吧!~~