#inclde<stdio.h>
void main(void)
{
char * str = "abc";
char newbuf[21];
sprintf(newbuf,"%020s",str);
printf("%s",newbuf);
}////////////output/////////////000000000000000000000abc

解决方案 »

  1.   

    请问你能不能将sprintf()的源代码写出来
      

  2.   

    老兄,sprintf()是标准库函数。就是将格式化的数据输出变量里。要源码的话向微软要!
      

  3.   

    thank insid1 and tigerfox very much!
      

  4.   

    BOOL normalizeN(char* buffer, int width, char fill)
    {
    int distance = width - strlen(buffer); if (distance < 0)
    return FALSE; // do not trunc if (distance > 0)
    {
    int end = (buffer[0] == '-') ? 1 : 0; for (int i = strlen(buffer); i >= end; i--)
    buffer[i+distance] = buffer[i];

    memset(buffer+end, fill, distance);
    } return TRUE;
    }then:
    normalizeN(buffer, 20, '0');
    OK
      

  5.   

    int __cdecl _snprintf (
            char *string,
            size_t count,
            const char *format,
            ...
            )
    #endif  /* _COUNT_ */{
            FILE str;
            REG1 FILE *outfile = &str;
            va_list arglist;
            REG2 int retval;        va_start(arglist, format);        _ASSERTE(string != NULL);
            _ASSERTE(format != NULL);        outfile->_flag = _IOWRT|_IOSTRG;
            outfile->_ptr = outfile->_base = string;
    #ifndef _COUNT_
            outfile->_cnt = MAXSTR;
    #else  /* _COUNT_ */
            outfile->_cnt = count;
    #endif  /* _COUNT_ */        retval = _output(outfile,format,arglist);        _putc_lk('\0',outfile); /* no-lock version */        return(retval);
    }
      

  6.   

    Sorry for losted ...Here is the all ./***
    *sprintf.c - print formatted to string
    *
    *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
    *
    *Purpose:
    *       defines sprintf() and _snprintf() - print formatted data to string
    *
    *******************************************************************************/#include <cruntime.h>
    #include <stdio.h>
    #include <dbgint.h>
    #include <stdarg.h>
    #include <internal.h>
    #include <limits.h>
    #include <mtdll.h>#define MAXSTR INT_MAX
    /***
    *ifndef _COUNT_
    *int sprintf(string, format, ...) - print formatted data to string
    *else
    *int _snprintf(string, cnt, format, ...) - print formatted data to string
    *endif
    *
    *Purpose:
    *       Prints formatted data to the using the format string to
    *       format data and getting as many arguments as called for
    *       Sets up a FILE so file i/o operations can be used, make
    *       string look like a huge buffer to it, but _flsbuf will
    *       refuse to flush it if it fills up.  Appends '\0' to make
    *       it a true string. _output does the real work here
    *
    *       Allocate the 'fake' _iob[] entry statically instead of on
    *       the stack so that other routines can assume that _iob[]
    *       entries are in are in DGROUP and, thus, are near.
    *
    *ifdef _COUNT_
    *       The _snprintf() flavor takes a count argument that is
    *       the max number of bytes that should be written to the
    *       user's buffer.
    *endif
    *
    *       Multi-thread: (1) Since there is no stream, this routine must
    *       never try to get the stream lock (i.e., there is no stream
    *       lock either). (2) Also, since there is only one statically
    *       allocated 'fake' iob, we must lock/unlock to prevent collisions.
    *
    *Entry:
    *       char *string - pointer to place to put output
    *ifdef _COUNT_
    *       size_t count - max number of bytes to put in buffer
    *endif
    *       char *format - format string to control data format/number
    *       of arguments followed by list of arguments, number and type
    *       controlled by format string
    *
    *Exit:
    *       returns number of characters printed
    *
    *Exceptions:
    *
    *******************************************************************************/#ifndef _COUNT_int __cdecl sprintf (
            char *string,
            const char *format,
            ...
            )
    #else  /* _COUNT_ */int __cdecl _snprintf (
            char *string,
            size_t count,
            const char *format,
            ...
            )
    #endif  /* _COUNT_ */{
            FILE str;
            REG1 FILE *outfile = &str;
            va_list arglist;
            REG2 int retval;        va_start(arglist, format);        _ASSERTE(string != NULL);
            _ASSERTE(format != NULL);        outfile->_flag = _IOWRT|_IOSTRG;
            outfile->_ptr = outfile->_base = string;
    #ifndef _COUNT_
            outfile->_cnt = MAXSTR;
    #else  /* _COUNT_ */
            outfile->_cnt = count;
    #endif  /* _COUNT_ */        retval = _output(outfile,format,arglist);        _putc_lk('\0',outfile); /* no-lock version */        return(retval);
    }