printf 仿真

q451157067 2011-10-03 03:14:11
需要一个类似于printf之类的函数.......可以接受随意参数 可以用 %s %d %f %ld占位符 ....
不知道怎么搞
...全文
113 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
q451157067 2011-10-03
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 mingliang1212 的回复:]

引用 8 楼 q451157067 的回复:

谢过 你上面那个可变参数的解释 足够了....printf的就不看了...
我想的是 可以直接写出sql语句 这个的好像正好不行....% %是sql 的格式

SELECT * FROM `item` WHERE `name` LIKE '%XX%
如果我传递的 是 SELECT * FROM `item` WHERE `name` ……
[/Quote]

额 我都搞定了...

不过C的 库 确实看的头疼...和STL比 他确实复杂了太多...不过STL好像也借用了C++的库吧...貌似有吧....
貌似 C++的底层的也是那样复杂吧~~~
柯本 2011-10-03
  • 打赏
  • 举报
回复
SQL的参数,又是各个数据库的SQL语言各不相同,参考下你用数据库的SQL手册

iamnobody 2011-10-03
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 q451157067 的回复:]

谢过 你上面那个可变参数的解释 足够了....printf的就不看了...
我想的是 可以直接写出sql语句 这个的好像正好不行....% %是sql 的格式

SELECT * FROM `item` WHERE `name` LIKE '%XX%
如果我传递的 是 SELECT * FROM `item` WHERE `name` LIKE '%%s%‘ ,s
正好悲剧
[/Quote]

扫了一眼printf调用的那个2000行左右的函数。。。也就是分析格式控制符“%d"之类的那个函数。头都大了。。。
现在才发现stl里的__Name__Something__ 之类的__ 多么的可爱。至少还可以勉强看得明白,但是这个c语言。。。里面一大堆的 abc 2000行啊。。
q451157067 2011-10-03
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 mingliang1212 的回复:]

首先要研究下这个:
http://wenku.baidu.com/view/03d73e0bf78a6529647d5329.html

要printf代码的话:
C/C++ code


Purpose:
* defines printf() - print formatted data
*
****************************************……
[/Quote]




谢过 你上面那个可变参数的解释 足够了....printf的就不看了...
我想的是 可以直接写出sql语句 这个的好像正好不行....% %是sql 的格式

SELECT * FROM `item` WHERE `name` LIKE '%XX%
如果我传递的 是 SELECT * FROM `item` WHERE `name` LIKE '%%s%‘ ,s
正好悲剧
柯本 2011-10-03
  • 打赏
  • 举报
回复
晕,少打个关键字

而各个编译器实现也尽相同
柯本 2011-10-03
  • 打赏
  • 举报
回复
对于可变参数,C与C++实现略有不同,而各个编译器实现也尽相同
好在一般的编译器都会提供printf的源码供参考,还有就是看编译器的文档都有介绍
对标准的C++,它有以下三个函数:
Header File

stdarg.h

Category

Variable Argument List Routines

Prototype

void va_start(va_list ap, lastfix);

type va_arg(va_list ap, type);

void va_end(va_list ap);

Description

Implement a variable argument list.

Some C functions, such as vfprintf and vprintf, take variable argument lists in addition to taking a number of fixed (known) parameters. The va_arg, va_end, and va_start macros provide a portable way to access these argument lists. They are used for stepping through a list of arguments when the called function does not know the number and types of the arguments being passed.

The header file stdarg.h declares one type (va_list) and three macros (va_start, va_arg, and va_end).

va_list: This array holds information needed by va_arg and va_end. When a called function takes a variable argument list, it declares a variable ap of type va_list.
va_start: This routine (implemented as a macro) sets ap to point to the first of the variable arguments being passed to the function. va_start must be used before the first call to va_arg or va_end.
va_start takes two parameters: ap and lastfix. (ap is explained under va_list in the preceding paragraph; lastfix is the name of the last fixed parameter being passed to the called function.)

va_arg: This routine (also implemented as a macro) expands to an expression that has the same type and value as the next argument being passed (one of the variable arguments). The variable ap to va_arg should be the same ap that va_start
initialized.

Note: Because of default promotions, you cannot use char, unsigned char, or float types with va_arg.

The first time va_arg is used, it returns the first argument in the list. Each successive time va_arg is used, it returns the next argument in the list. It does this by first dereferencing ap, and then incrementing ap to point to the following item. va_arg uses the type to both perform the dereference and to locate the following item. Each successive time va_arg is invoked, it modifies ap to point to the next argument in the list.

va_end: This macro helps the called function perform a normal return. va_end might modify ap in such a way that it cannot be used unless va_start is recalled. va_end should be called after va_arg has read all the arguments; failure to do so might cause strange, undefined behavior in your program.

Return Value

va_start and va_end return no values; va_arg returns the current argument in the list (the one that ap is pointing to).
  • 打赏
  • 举报
回复
楼主,其实最最简单的 你可以自己实现一个printf
但是你自己实现的要有以下功能:
一、支持同步异步
二、支持类型检查

同步异步可以通过 lock来实现,但是类型检查Windows不知道怎么来实现
倒是在Linux下,可以通过以下方式来做

inline int myprintf( const char *fmt, ... )
__attribute__ ((format (printf, 1, 2)));
iamnobody 2011-10-03
  • 打赏
  • 举报
回复
首先要研究下这个:
http://wenku.baidu.com/view/03d73e0bf78a6529647d5329.html

要printf代码的话:


Purpose:
* defines printf() - print formatted data
*
*******************************************************************************/

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

/***
*int printf(format, ...) - print formatted data
*
*Purpose:
* Prints formatted data on stdout using the format string to
* format data and getting as many arguments as called for
* Uses temporary buffering to improve efficiency.
* _output does the real work here
*
*Entry:
* 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:
*
*******************************************************************************/

int __cdecl printf (
const char *format,
...
)
/*
* stdout 'PRINT', 'F'ormatted
*/
{
va_list arglist;
int buffing;
int retval;

_VALIDATE_RETURN( (format != NULL), EINVAL, -1);

va_start(arglist, format);

_lock_str2(1, stdout);
__try {
buffing = _stbuf(stdout);

retval = _output_l(stdout,format,NULL,arglist);

_ftbuf(buffing, stdout);

}
__finally {
_unlock_str2(1, stdout);
}

return(retval);
}

int __cdecl _printf_l (
const char *format,
_locale_t plocinfo,
...
)
{
va_list arglist;

va_start(arglist, plocinfo);

return _vprintf_l(format, plocinfo, arglist);
}


int __cdecl _printf_s_l (
const char *format,
_locale_t plocinfo,
...
)
{
va_list arglist;

va_start(arglist, plocinfo);

return _vprintf_s_l(format, plocinfo, arglist);
}

int __cdecl printf_s (
const char *format,
...
)
{
va_list arglist;

va_start(arglist, format);

return _vprintf_s_l(format, NULL, arglist);
}

int __cdecl _printf_p_l (
const char *format,
_locale_t plocinfo,
...
)
{
va_list arglist;

va_start(arglist, plocinfo);

return _vprintf_p_l(format, plocinfo, arglist);
}

int __cdecl _printf_p (
const char *format,
...
)
{
va_list arglist;

va_start(arglist, format);

return _vprintf_p_l(format, NULL, arglist);
}

static UINT_PTR __enable_percent_n = 0;

/***
*int _set_printf_count_output(int)
*
*Purpose:
* Enables or disables %n format specifier for printf family functions
*
*Internals:
* __enable_percent_n is set to (__security_cookie|1) for security reasons;
* if set to a static value, an attacker could first modify __enable_percent_n
* and then provide a malicious %n specifier. The cookie is ORed with 1
* because a zero cookie is a possibility.
******************************************************************************/
int __cdecl _set_printf_count_output(int value)
{
int old = (__enable_percent_n == (__security_cookie | 1));
__enable_percent_n = (value ? (__security_cookie | 1) : 0);
return old;
}

/***
*int _get_printf_count_output()
*
*Purpose:
* Checks whether %n format specifier for printf family functions is enabled
******************************************************************************/
int __cdecl _get_printf_count_output()
{
return ( __enable_percent_n == (__security_cookie | 1));
}


q451157067 2011-10-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 qq120848369 的回复:]

找%号,然后走switch,从而在参数栈上怎么解析,之后做多少偏移。
[/Quote]
我也是这么想得.....有没有办法 直接用printf 的码 改
q451157067 2011-10-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 qq120848369 的回复:]

找%号,然后走switch,从而在参数栈上怎么解析,之后做多少偏移。
[/Quote]
我也是这么想得.....有没有办法 直接用printf 的码 改
qq120848369 2011-10-03
  • 打赏
  • 举报
回复
找%号,然后走switch,从而在参数栈上怎么解析,之后做多少偏移。

64,654

社区成员

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

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