Chinaunix首页 | 论坛 | 博客
  • 博客访问: 129080
  • 博文数量: 40
  • 博客积分: 2228
  • 博客等级: 大尉
  • 技术积分: 335
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-12 11:48
文章分类

全部博文(40)

文章存档

2011年(11)

2010年(29)

我的朋友

分类: LINUX

2010-07-18 23:16:51

  char   temp[256];   
  sprintf(temp,"%d",value);
下面是转换的实例:
 

#include<stdio.h>
#include<fcntl.h>
#include<syscall.h>
#define PERMS 0777

void error(char *,...);

main(int argc, char *argv[])
{
    int f1, f2, n;
    char buf[BUFSIZ];

    if(argc != 3)
        error("Usage: cp from to");
    if((f1 = open(argv[1], O_RDONLY, 0)) == -1)
        error("cp: can't open %s",argv[1])
//the first method ,set the output length.

/*
    while ((n = read(f1, buf, BUFSIZ)) > 0){
        buf[n] = '\0';
        printf("-->%s",buf);
    }
*/

//the second method

    char formatstr[20];
    while ((n = read(f1, buf, BUFSIZ)) > 0){
        sprintf(formatstr,"-->%%.%ds",n);
        printf(formatstr,buf);
    }
    if((f1 = open(argv[1], O_RDONLY, 0)) == -1)
        error("cp: can't open %s",argv[1]);
    if((f2 = creat(argv[2], PERMS)) == -1)
        error("cp: can't create %s,mode %03o",
            argv[2],PERMS);
    while ((n = read(f1, buf, BUFSIZ)) > 0)
        if(write(f2, buf, n) != n)
            error("cp: write error onfile %s",argv[2]);
    return 0;
}


阅读(984) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~