C/C++
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   Web Development Archives FAQs C/C++

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Web Development Archives Sponsor:
  #1  
Old July 3rd, 2008, 06:09 PM
Tarique
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Custom Scanf Routine

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!

Reply With Quote
  #2  
Old July 3rd, 2008, 09:30 PM
viza
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Custom Scanf Routine

Hi

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

I have tried to write my custom scanf function

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

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

Reply With Quote
  #3  
Old July 4th, 2008, 08:00 AM
Tarique
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Custom Scanf Routine

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 ))) && (EF != 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("Input Stream Error");
clearerr( stdin );
return "I/";
}
if(feof(stdin)) {
perror("EF Encountered");
clearerr( stdin );
return "EF";
}
}
else{
if(!((*p == '$') || ( (*p == '\0') && (* == '\n') ))) {
flushln(stdin);
puts("Too long input!\n\n");
return NULL;
}
}
strcpy(buff,buffer);
return buff;
}

/*Accept a string and pass a valid long int if possible,else return 0*/
long getInt(void)
{
char intbuff[100];
char *buffptr = intbuff;
char *end_ptr;
long int lVal;
int trial = 1; /*No of tries on wrong input*/
int dist=0;
errno = 0;

while(trial-- != 0)
{
buffptr = input("Enter :",intbuff);
if (buffptr == "I/" || buffptr == "EF" || buffptr == NULL)
return 0;

lVal= strtol(buffptr, &end_ptr, 0);
if (ERANGE == errno){
perror(" of Range");
}
else if (lVal INT_MAX){
perror("Too Large!");
}
else if (lVal < INT_MIN){
perror("Too Small");
}
else if (end_ptr == buffptr){
printf("Not a Valid Integer\n\n");
}
else
return lVal;
}
return 0;
}

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 =(int)getInt()){
*va_arg(ap,long *) = temp;/* Direct Memory Access to MEM */
count ++;
}
else break;
break;

case 'l':
if((p) == 'd'){
if(temp =getInt()){
*va_arg(ap,long *) = temp;/* Direct Memory Access to MEM */
count ++;
p--;
}
else {
*va_arg(ap,long *) = 0;/*Invalid input ,so set variable to zero*/
p--;
break;
}
}
break;
default:
break;
}
}
va_end(ap);
return count;
}

int main(void)
{
long int a = 0,b = 0;
int c = 0,d = 0;

myscanf("%ld %ld",&a,&b);
printf("%ld %ld \n\n",a,b);

myscanf("%d %d",&c,&d);
printf("%d %d",c,d);

return 0;
}

Reply With Quote
  #4  
Old July 4th, 2008, 09:19 AM
viza
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Custom Scanf Routine

Fri, 04 Jul 2008 18:10:51 +0530, Tarique wrote:

I would be really grateful if someone can review it.Comments awaited!

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

Generally it's not smart to use arbitrary fixed length buffers. Are you
certain that the input won't be bigger? If your code is well written
then it that would cause it to fail, and if it is not well written then
it could FEYC (fing explode your computer).

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

Gah! ugly. It's best that you don't use assignments as truth values,
especially as a beginner. That continue is superfluous too. I would
also ungetc() the character and return void.

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

char *p= buffer + BUFSIZE - 1; might be easier to read.

if(message != "")

You can't do that in C. Well, you can, but it doesn't mean what you
think. Lookup the strcmp() function.

puts(message);
buffer[BUFFSIZE -1] = '$';
if( fgets(buffer,BUFFSIZE,stdin) == NULL) {
if(ferror(stdin)) {
perror("Input Stream Error");
clearerr( stdin );
return "I/";

It's not usual to return strings like that one. In case of error perhaps
you might want to return NULL.

}
if(feof(stdin)) {
perror("EF Encountered");
clearerr( stdin );
return "EF";
}
}
else{
if(!((*p == '$') || ( (*p == '\0') && (* == '\n') )))
{
flushln(stdin);
puts("Too long input!\n\n");
return NULL;
}
}
strcpy(buff,buffer);

If you know that buff is at least as big as buffer, why do you need
buffer in the first place? Read to buff.

return buff;
}
>

/*Accept a string and pass a valid long int if possible,else return 0*/
long getInt(void)
{
char intbuff[100];
char *buffptr = intbuff;
char *end_ptr;
long int lVal;
int trial = 1; /*No of tries on wrong input*/ int dist=0;
errno = 0;
>

while(trial-- != 0)
{
buffptr = input("Enter :",intbuff);
if (buffptr == "I/" || buffptr == "EF" || buffptr == NULL)
return 0;
>

lVal= strtol(buffptr, &end_ptr, 0);
if (ERANGE == errno){

You didn't set errno to zero. Well you did, but it might have changed
many times since then. Do it immediately before each conversion.

I haven't gome through it all, but HTH
viza

Reply With Quote
  #5  
Old July 4th, 2008, 09:19 AM
santosh
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Custom Scanf Routine

Tarique wrote:

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

Just a note. Haven't seen it in detail, sorry.

Also in future please use spaces in place of tabs when posting to
Usenet. As you can see below, some software on the Usenet stripped out
all your tabs, rendering unformatted code, which is very difficult to
read.

#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 ))) && (EF != 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("Input Stream Error");
clearerr( stdin );
return "I/";
}
if(feof(stdin)) {
perror("EF Encountered");
clearerr( stdin );
return "EF";
}
}
else{
if(!((*p == '$') || ( (*p == '\0') && (* == '\n') ))) {
flushln(stdin);
puts("Too long input!\n\n");
return NULL;
}
}
strcpy(buff,buffer);
return buff;
}
>

/*Accept a string and pass a valid long int if possible,else return
0*/ long getInt(void)
{
char intbuff[100];
char *buffptr = intbuff;
char *end_ptr;
long int lVal;
int trial = 1; /*No of tries on wrong input*/
int dist=0;
errno = 0;
>

while(trial-- != 0)
{
buffptr = input("Enter :",intbuff);
if (buffptr == "I/" || buffptr == "EF" || buffptr == NULL)
return 0;
>

lVal= strtol(buffptr, &end_ptr, 0);
if (ERANGE == errno){
perror(" of Range");
}
else if (lVal INT_MAX){
perror("Too Large!");
}
else if (lVal < INT_MIN){
perror("Too Small");
}

What will you do on implementations where INT_MAX == LNG_MAX and
INT_MIN == LNG_MIN?

<snip rest>


Reply With Quote
  #6  
Old July 4th, 2008, 08:19 PM
viza
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Custom Scanf Routine

Fri, 04 Jul 2008 19:48:08 -0400, CBFalconer wrote:

viza wrote:
>Tarique wrote:


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

>Gah! ugly. It's best that you don't use assignments as truth values,
>especially as a beginner. That continue is superfluous too. I would
>also ungetc() the character and return void.
>

Not in the least ugly.

I wouldn't take it home.

Apart from the awkward location of the initial comment. Works like a
charm, too. The continue prevents misreading an isolated semi.

Your ungetc recommendation would make the routine fail to perform the
function indicated by the name.

True. I misread the ugly loop construct and thought it read one-past the
newline.



Reply With Quote
Reply

Viewing: Web Development Archives FAQs C/C++ > Custom Scanf Routine


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway