对scanf函数进行封装,

xyuz 2013-03-26 03:09:06

#include "stdio.h"

void mscanf(char *format, ...)
{
scanf(format);
}

int main()
{
int n1, n2;
mscanf("%d%d", &n1, &n2);
printf("%d,%d\n", n1, n2);
return 0;
}


我大概是想封装一层在函数里调用scanf, 这样写哪里有问题?
...全文
262 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-03-27
  • 打赏
  • 举报
回复
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\scanf.c
/***
*scanf.c - read formatted data from stdin
*
*       Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*       defines scanf() - reads formatted data from stdin
*
*******************************************************************************/

#include <cruntime.h>
#include <stdio.h>
#include <dbgint.h>
#include <stdarg.h>
#include <file2.h>
#include <internal.h>
#include <mtdll.h>

/***
*int vscanf(format, ...) - read formatted data from stdin
*
*Purpose:
*       This is a helper function to be called from fscanf & fscanf_s
*
*Entry:
*       INPUTFN inputfn - scanf & scanf_s pass either _input_l or _input_s_l
*                   which is then used to do the real work.
*       char *format - format string
*       va_list arglist - arglist of output pointers
*
*Exit:
*       returns number of fields read and assigned
*
*Exceptions:
*
*******************************************************************************/

int __cdecl vscanf (
        INPUTFN inputfn,
        const char *format,
        _locale_t plocinfo,
        va_list arglist
        )
/*
 * stdin 'SCAN', 'F'ormatted
 */
{
    int retval;

    _VALIDATE_RETURN( (format != NULL), EINVAL, EOF);

    _lock_str2(0, stdin);
    __try {
        retval = (inputfn(stdin, format, plocinfo, arglist));
    }
    __finally {
        _unlock_str2(0, stdin);
    }

    return(retval);
}

/***
*int scanf(format, ...) - read formatted data from stdin
*
*Purpose:
*       Reads formatted data from stdin into arguments.  _input_l does the real
*       work here.
*
*Entry:
*       char *format - format string
*       followed by list of pointers to storage for the data read.  The number
*       and type are controlled by the format string.
*
*Exit:
*       returns number of fields read and assigned
*
*Exceptions:
*
*******************************************************************************/
int __cdecl scanf (
        const char *format,
        ...
        )
{
        va_list arglist;
        va_start(arglist, format);
        return vscanf(_input_l, format, NULL, arglist);
}

int __cdecl _scanf_l (
        const char *format,
        _locale_t plocinfo,
        ...
        )
{
        va_list arglist;
        va_start(arglist, plocinfo);
        return vscanf(_input_l, format, plocinfo, arglist);
}

/***
*int scanf_s(format, ...) - read formatted data from stdin
*
*   Same as scanf above except that it calls _input_s_l to do the real work.
*   _input_s_l has a size check for array parameters.
*
*******************************************************************************/
int __cdecl scanf_s (
        const char *format,
        ...
        )
{
        va_list arglist;
        va_start(arglist, format);
        return vscanf(_input_s_l, format, NULL, arglist);
}

int __cdecl _scanf_s_l (
        const char *format,
        _locale_t plocinfo,
        ...
        )
{
        va_list arglist;
        va_start(arglist, plocinfo);
        return vscanf(_input_s_l, format, plocinfo, arglist);
}
xyuz 2013-03-27
  • 打赏
  • 举报
回复
我的意思是如何传变长参数 我原本是封装fscanf的省略fopen 原理大概弄明白了, 变长参数再传参不能直接传 得这样

va_list args;
va_start(args, format);
vscanf(format, args);
va_end(args);
mujiok2003 2013-03-26
  • 打赏
  • 举报
回复
方法一:使用宏

#define mscanf scanf
方法二: 使用va_list, va_start, va_end, va_arg等宏,参考我刚写的这篇(我只实现了printf),你可以修改一下就能用。
starytx 2013-03-26
  • 打赏
  • 举报
回复
很霸气的封装啊,还是先了解一下不定参数的使用吧,这里是一个例子
#include <stdarg.h>
/* 函数名:max
* 功能:返回n个整数中的最大值
* 参数:num:整数的个数 ...:num个输入的整数
* 返回值:求得的最大整数
*/
int MyMax ( int num, ... )
{ 
    int m = -0x7FFFFFFF; /* 32系统中最小的整数 */ 
    va_list ap; 
    va_start ( ap, num ); 
    for ( int i= 0; i< num; i++ ) 
    {  
        int t = va_arg (ap, int);  
        if ( t > m )  
        {  
            m = t; 
        } 
    } 
    va_end (ap); 
    return m;
} 


int _tmain(int argc, _TCHAR* argv[])
{
     int n = MyMax ( 5, 5, 6 ,3 ,8 ,5); /* 求5个整数中的最大值 */ 
     cout << n << endl;

    return 0;
}
lin5161678 2013-03-26
  • 打赏
  • 举报
回复
你传入myscanf 的 &n1, &n2 没有传给scanf
shen_wei 2013-03-26
  • 打赏
  • 举报
回复
这样也叫封装?希望你能看看多参数函数
cyneuzk 2013-03-26
  • 打赏
  • 举报
回复
我想知道这样跟你用scanf有什么区别 可以看一下int scanf(char *format[,argument,...]);函数原型

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧