473,473 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Handy hint, length of string scanfed.


Here is a handy hint to extract the length of input consumed by scanf().
(Returns -1 on a bad match / memory failure).

static int int_sscanf(const char *src, const char *fmt)
{
char *fmt2;
int i;
int j =0;
int answer = -1;

fmt = malloc(strlen(fmt) * 2 + 3);
if(!fmt)
return -1;
for(i=0;fmt[i];i++)
{
if(fmt[i] == '%')
{
if(fmt[i+1] == '%')
{
fmt2[j++] = '%';
fmt2[j++] = '%';
i++;
}
else if(fmt[i+1] == '*')
{
fmt2[j++] = '%';
fmt2[j++] = '*';
i++;
}
else
{
fmt2[j++] = '%';
fmt2[j++] = '*';
}
}
fmt2[j++] = fmt[i];
}
fmt2[j++] = '%';
fmt2[j++] = 'n';
fmt2[j++] = 0;

sscanf(src, fmt2, &answer);
free(fmt2);
return answer;
}
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Sep 22 '07 #1
4 1375
Malcolm McLean wrote:
>
Here is a handy hint to extract the length
of input consumed by scanf().
(Returns -1 on a bad match / memory failure).

static int int_sscanf(const char *src, const char *fmt)
{
char *fmt2;
int i;
int j =0;
int answer = -1;

fmt = malloc(strlen(fmt) * 2 + 3);
if(!fmt)
return -1;
for(i=0;fmt[i];i++)
{
if(fmt[i] == '%')
{
if(fmt[i+1] == '%')
{
fmt2[j++] = '%';
fmt2[j++] = '%';
i++;
}
else if(fmt[i+1] == '*')
{
fmt2[j++] = '%';
fmt2[j++] = '*';
i++;
}
else
{
fmt2[j++] = '%';
fmt2[j++] = '*';
}
}
fmt2[j++] = fmt[i];
}
fmt2[j++] = '%';
fmt2[j++] = 'n';
fmt2[j++] = 0;

sscanf(src, fmt2, &answer);
free(fmt2);
return answer;
}
I can't figure out how to use it.
This program crashes on the int_sscanf function call.

/* BEGIN int_sscanf.c */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

static int int_sscanf(const char *src, const char *fmt);

int main(void)
{
const char src[] = "INT_MAX";
const char *fmt = "%s";
char dest[sizeof src];
int rc;

rc = sscanf(src, fmt, dest);
printf("rc is %d\n", rc);
puts(dest);
rc = int_sscanf(src, fmt);
printf("rc is %d\n", rc);
return 0;
}

static int int_sscanf(const char *src, const char *fmt)
{
char *fmt2;
int i;
int j =0;
int answer = -1;

fmt = malloc(strlen(fmt) * 2 + 3);
if(!fmt)
return -1;
for(i=0;fmt[i];i++)
{
if(fmt[i] == '%')
{
if(fmt[i+1] == '%')
{
fmt2[j++] = '%';
fmt2[j++] = '%';
i++;
}
else if(fmt[i+1] == '*')
{
fmt2[j++] = '%';
fmt2[j++] = '*';
i++;
}
else
{
fmt2[j++] = '%';
fmt2[j++] = '*';
}
}
fmt2[j++] = fmt[i];
}
fmt2[j++] = '%';
fmt2[j++] = 'n';
fmt2[j++] = 0;

sscanf(src, fmt2, &answer);
free(fmt2);
return answer;
}

/* END int_sscanf.c */

--
pete
Sep 22 '07 #2
"Malcolm McLean" <re*******@btinternet.comwrites:
Here is a handy hint to extract the length of input consumed by scanf().
(Returns -1 on a bad match / memory failure).
static int int_sscanf(const char *src, const char *fmt)
{
char *fmt2;
int i;
int j =0;
int answer = -1;

fmt = malloc(strlen(fmt) * 2 + 3);
fmt2
if(!fmt)
ditto.
return -1;
for(i=0;fmt[i];i++)
{
if(fmt[i] == '%')
{
if(fmt[i+1] == '%')
{
fmt2[j++] = '%';
fmt2[j++] = '%';
i++;
}
else if(fmt[i+1] == '*')
{
fmt2[j++] = '%';
fmt2[j++] = '*';
i++;
}
else
{
fmt2[j++] = '%';
fmt2[j++] = '*';
}
}
fmt2[j++] = fmt[i];
missing else? Goes wrong without it (i.e. with just the fixes above).
}
fmt2[j++] = '%';
fmt2[j++] = 'n';
fmt2[j++] = 0;

sscanf(src, fmt2, &answer);
free(fmt2);
return answer;
}
Then you need to fix cases like "%[%]".

I was going to add that training % might be a problem, but I am not
sure (on reading the standard) if it is legal. It seems not.

--
Ben.
Sep 22 '07 #3

"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
"Malcolm McLean" <re*******@btinternet.comwrites:
Then you need to fix cases like "%[%]".

I was going to add that training % might be a problem, but I am not
sure (on reading the standard) if it is legal. It seems not.
The square brackets are fiddly. I think this fixes them up, plus the other
problems caused by an old edit being posted.

int int_sscanf(const char *src, char *fmt)
{
char *fmt2;
int i;
int j =0;
int answer = -1;
int inbrackets = 0;
char *temp;

fmt2 = malloc(strlen(fmt) * 2 + 3);
if(!fmt2)
return -1;
for(i=0;fmt[i];i++)
{
if(fmt[i] == '%' && !inbrackets)
{
temp = &fmt[i+1];
while(isdigit((unsigned char) *temp) || *temp == '*')
temp++;
if(*temp == '[')
inbrackets = 1;
if(fmt[i+1] == '%')
{
fmt2[j++] = '%';
fmt2[j++] = '%';
i++;
}
else if(fmt[i+1] == '*')
{
fmt2[j++] = '%';
fmt2[j++] = '*';
i++;
}
else
{
fmt2[j++] = '%';
fmt2[j++] = '*';
}
}
else if(fmt[i] == ']')
{
if( inbrackets && fmt[i-1] != '[' && !(fmt[i-1] == '^' && fmt[i-2] ==
'[') )
inbrackets = 0;
fmt2[j++] = ']';
}
else
fmt2[j++] = fmt[i];
}
fmt2[j++] = '%';
fmt2[j++] = 'n';
fmt2[j++] = 0;

sscanf(src, fmt2, &answer);
free(fmt2);
return answer;
}

Sep 22 '07 #4
"Malcolm McLean" <re*******@btinternet.comwrites:
"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
>"Malcolm McLean" <re*******@btinternet.comwrites:
Then you need to fix cases like "%[%]".

I was going to add that training % might be a problem, but I am not
sure (on reading the standard) if it is legal. It seems not.

The square brackets are fiddly. I think this fixes them up, plus the
other problems caused by an old edit being posted.

int int_sscanf(const char *src, char *fmt)
The old fmt parameter was const char *, which seems the right choice.
--
Ben.
Sep 22 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: John Smith | last post by:
Hi, I'm writing a library in C++ which is supposed to be used by people using C. One function I have must return a string to users which is arbitrary length. The user must be able to use this...
3
by: rudymoore | last post by:
I'm often find-ing the same string in a map<string,xxx> I want to pass a hint to map::find but was disappointed to find that only map::insert takes the hint. Why would map::insert take a hint...
6
by: Mark P | last post by:
Some time ago I posted here about inserting into a set with a hint: ...
7
by: piperzen | last post by:
Hi, I'm developing a set of language learning exercises for http://www.yale.edu/swahili , and we've run into a javascript question that the programmer doesn't know how to tackle (we mostly work...
3
by: Jimski | last post by:
Hello all, I am having a problem where I get an error message when I call FlushFinalBlock when decrypting my encrypted text. I am using the Rijndael algorithm. The error message is "Length...
10
by: Mavenos | last post by:
Hi Web Masters, Just wondering wether you can help us to come up with some tokenize script. My problem is wanted to display a LONG content into a short para (by giving minimum letter lenght)...
0
by: HKSHK | last post by:
This list compares the error codes used in VB.NET 2003 with those used in VB6. Error Codes: ============ 3: This Error number is obsolete and no longer used. (Formerly: Return without GoSub)...
1
by: Sathyaish | last post by:
I have the following scenario: Algorithm: 3DES Cipher Mode: CBC Key Size: 128-bit Block Size: 64 bit IV: 0x0000000000000000 (an eight byte array of zeros) The results I get using .NET with...
1
by: Rick Knospler | last post by:
I am trying to convert a vb6 project to vb.net. The conversion worked for the most part except for the fixed length strings and fixed length string arrays. Bascially the vb6 programmer stored all...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.