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

全部博文(134)

文章存档

2015年(2)

2014年(4)

2013年(128)

分类: C/C++

2013-10-22 19:26:32

/*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);
*/
#include
#include
#include
#include
#include
#include
#include
int main()
{
    char buf[100] = {};
    int i = 0, j = 0;
    char * p = NULL;
    char * q = NULL;
    int fd = -1;
    for(i = 0; i < 10; i++)
    {
        sprintf(buf,"%d.txt",i+1);
        fd = open(buf,O_RDWR|O_TRUNC|O_CREAT,0755);
        if(fd < 0)
        {
            perror("open");
            _exit(1);
        }
        lseek(fd,41-1,SEEK_SET);
        write(fd, " ",1);
        p = mmap(NULL,41,PROT_WRITE,MAP_SHARED,fd,0);
        if(p == MAP_FAILED)
        {
            perror("mmap");
            exit(1);
        }
        close(fd);
        for(q = p,j = 1; j <= 10; j++)
        {
            sprintf(buf,"%03d ", i * 10 + j);
            memcpy(q, buf, strlen(buf));
            q += strlen(buf);
        }
        memcpy(q,"\n",1);
        munmap(p,41);
    }
    return 0;
}
//------------------------我是分割线----------
/*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);
*/
#include
#include
#include
#include
#include
#include
#include
int main()
{
    char buf[100] = {};
    int i = 0, j = 0;
    char * p = NULL;
    char * q = NULL;
    int fd = -1;
    for(i = 0; i < 10; i++)
    {
        sprintf(buf,"%d.txt",i+1);
        fd = open(buf,O_RDWR|O_TRUNC|O_CREAT,0755);
        if(fd < 0)
        {
            perror("open");
            _exit(1);
        }
        lseek(fd,41-1,SEEK_SET);
        write(fd, " ",1);
        p = mmap(NULL,41,PROT_WRITE,MAP_SHARED,fd,0);
        if(p == MAP_FAILED)
        {
            perror("mmap");
            exit(1);
        }
        close(fd);
        for(q = p,j = 1; j <= 10; j++)
        {
            sprintf(buf,"%03d ", i * 10 + j);
            memcpy(q, buf, strlen(buf));
            q += strlen(buf);
        }
        memcpy(q,"\n",1);
        munmap(p,41);
    }
    return 0;
}
//------------------------我是分割线----------


阅读(1127) | 评论(0) | 转发(0) |
0

上一篇:sprintf用法

下一篇:read write 函数

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