自定义Scanf例程 [英] Custom Scanf Routine

查看:55
本文介绍了自定义Scanf例程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾尝试在K& R2中提供的minprintf

行中编写自定义scanf函数。在函数myscanf()中我直接访问内存

using传递给该函数的地址。可能很危险吗?


虽然我得到了正确的输出。感谢任何帮助。


/ *包含文件* /


/ *辅助功能* /

int flushln(FILE * f){/ * Code * /}

char * input(char * message){/ * Code * /}

static int getInt(void){/ * Code * /}

/ * My自定义扫描程序* /


int myscanf(const char * format,...)

{

va_list ap; < br $>
const char * p;

int count = 0;

int temp;


va_start( ap,format);

for(p = format; * p; p ++){

if(* p!=''%''){

继续;

}

开关(* ++ p){

案例''d'':

if(temp = getInt()){

*(long *)va_arg(ap ,int)= temp; / *直接内存访问* /

count ++;

}

else {puts(" Input Error" ); }

休息;

}

}

va_end(ap);

返回计数;

}


int main(无效)

{

int a = 0 ,b = 0;

myscanf("%d%d",& a,& b);

printf("%d%d", a,b);


返回0;

}


谢谢!

I have tried to write my custom scanf function on the lines of minprintf
provided in K&R2.In the function myscanf() i access memory directly
using the address passed to the function .Can it be dangerous ?

I am getting the correct output though.Any help is appreciated.

/*Include Files*/

/*Assisting Functions*/
int flushln(FILE *f){ /*Code*/}
char *input(char *message){/*Code*/}
static int getInt(void){/*Code*/}
/*My Custom Scanf Routine*/

int myscanf(const char* format,...)
{
va_list ap;
const char *p;
int count = 0;
int temp;

va_start(ap,format);
for( p = format ; *p ;p++) {
if( *p != ''%''){
continue;
}
switch(*++p){
case ''d'':
if(temp = getInt()){
*(long *)va_arg(ap,int) = temp;/*Direct Memory Access*/
count ++;
}
else{ puts("Input Error"); }
break;
}
}
va_end(ap);
return count;
}

int main(void)
{
int a = 0,b = 0;
myscanf("%d %d",&a,&b);
printf("%d %d",a,b);

return 0;
}

Thanks!

推荐答案




On Fri,2008年7月4日03:12:09 +0530,Tarique写道:
Hi

On Fri, 04 Jul 2008 03:12:09 +0530, Tarique wrote:

我试图编写自定义scanf函数
I have tried to write my custom scanf function


*(long *)va_arg(ap,int) = temp;
*(long *)va_arg(ap,int) = temp;



这是一个非常糟糕的主意,它可能适用于指针

与整数大小相同的某些系统,但这通常是不是这样的。


即使这样做一次,ap上的下一个操作可能会失败。


va_arg需要告诉类型在调用函数时写入的参数是什么?
...你应该总是把第二个参数中的正确类型设为

。在大多数情况下,这意味着你

不需要投回报。


* va_arg(ap,long *)= temp;


将完美地工作,只要一个长指针真的写在参数列表中的当前位置




HTH

viza

That is a very bad idea, it might work on certain systems where pointers
are the same size as integers, but that is often not the case.

Even if this does work once, the next operation on ap might fail.

va_arg needs to be told the type of the argument that was written in
place of the ... in when the function was called. You should always put
the correct type in the second argument. In most cases that means you
won''t need to cast the return.

* va_arg(ap,long*) = temp;

will work perfectly, as long as a long pointer was really written at the
current place in the argument list.

HTH
viza


Tarique写道:

.... snip ...


这是我的最小自定义scanf函数的代码(用于输入有效的

ints n longs并进行错误检查)

我将非常感激如果有人可以复习。等待评论!


谢谢


#include< errno.h>

#include< stdio.h>

#include< stdlib.h>

#include< string.h>

#include < limits.h>

#include< stdarg.h>


#define BUFFSIZE 98 + 2 / *由input()使用* /


/ *如果需要,清除输入流* /

int flushln(FILE * f){

int ch;

while((''\ n''!=(ch = getc(f)))&& (EOF!= ch))

继续;

返回ch;

}


/ *接受来自用户的字符串(稍后解析)* /

char * input(const char * message,char * buff)

{

char buffer [BUFFSIZE];

char * p =& buffer [BUFFSIZE-1];


if(message!="")

put(消息);


buffer [BUFFSIZE -1] =''
Tarique wrote:
....snip...

This is my code for a minimal custom scanf function(for entering valid
ints n longs with error checking)
I would be really grateful if someone can review it.Comments awaited!

Thank You

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdarg.h>

#define BUFFSIZE 98 + 2 /*Used by input()*/

/*Clear The Input stream if required*/
int flushln(FILE *f) {
int ch;
while ((''\n'' != (ch = getc( f ))) && (EOF != ch))
continue;
return ch;
}

/*Accept a string from user (parse later on)*/
char *input(const char* message,char *buff)
{
char buffer[ BUFFSIZE ];
char *p = &buffer[ BUFFSIZE-1 ];

if(message != "")
puts(message);

buffer[BUFFSIZE -1] = ''


'';


if(fgets(buffer,BUFFSIZE,stdin)== NULL){

if(ferror(stdin)){

perror (输入流错误);

clearerr(stdin);

返回I / O;

}

if(feof(stdin)){

perror(EOF Encountered);

clearerr(stdin);

返回EOF;

}

}

else {

if(!((* p ==''
'';

if( fgets(buffer,BUFFSIZE,stdin) == NULL) {
if(ferror(stdin)) {
perror("Input Stream Error");
clearerr( stdin );
return "I/O";
}
if(feof(stdin)) {
perror("EOF Encountered");
clearerr( stdin );
return "EOF";
}
}
else{
if(!((*p == ''


这篇关于自定义Scanf例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆