473,789 Members | 2,476 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

scanf() help

Stu
I have the following "C" program, which works fine.

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

int
main()
{
char *buffer = "1234 - 5678";
int n, from, to;

n = sscanf (buffer, "%d%*s%d", &from, &to);

printf ("n = %d from = %d, to = %d\n", n, from, to);

return (0);
}

When I change the vaule of buffer to char *buffer = "1234-5678"; (note
I removed the spaces between '-') I need to change my scanf() to

n = sscanf (buffer, "%d%*c%d", &from, &to); /*change from %s to %c */

Can somebody please provide me with one scanf() statement that can handle
both spaces and non spaces between my values. The man page on Solaris did not
provide me with much direction thats why I am posting my question here.
Thanks in advance to all that answer
Nov 14 '05 #1
2 2897
Stu wrote:
I have the following "C" program, which works fine.

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

int
main()
{
char *buffer = "1234 - 5678";
int n, from, to;

n = sscanf (buffer, "%d%*s%d", &from, &to);

printf ("n = %d from = %d, to = %d\n", n, from, to);

return (0);
}

When I change the vaule of buffer to char *buffer = "1234-5678"; (note
I removed the spaces between '-') I need to change my scanf() to

n = sscanf (buffer, "%d%*c%d", &from, &to); /*change from %s to %c */

Can somebody please provide me with one scanf() statement that can handle
both spaces and non spaces between my values. The man page on Solaris did not
provide me with much direction thats why I am posting my question here.


It sounds like you need "%*[- ]" (perhaps with additional
"don't care" characters listed). Note that since '-' is one
of the characters you want to skip, there'll be no way to get
a negative `to' value.

--
Er*********@sun .com

Nov 14 '05 #2
>Stu wrote:
[snippage]
char *buffer = "1234 - 5678";
int n, from, to;

n = sscanf (buffer, "%d%*s%d", &from, &to); [or] When I change the vaule of buffer to char *buffer = "1234-5678"; ...
n = sscanf (buffer, "%d%*c%d", &from, &to); /*change from %s to %c */

Can somebody please provide me with one scanf() statement that can handle
both spaces and non spaces between my values. ...

In article <news:ck******* ***@news1brm.Ce ntral.Sun.COM>
Eric Sosman <er*********@su n.com> wrote: It sounds like you need "%*[- ]" (perhaps with additional
"don't care" characters listed). Note that since '-' is one
of the characters you want to skip, there'll be no way to get
a negative `to' value.


This will do the trick, but note that the %[ directive matches
*any* nonempty sequence of *any* of the characters in the scanset
(here just '-' and ' '), so this matches:

1234---5678

and:

1234 - -- - 5678

and:

1234- -5678

(which, as Eric Sosman noted, means there will be no negative "to"
values).

Another alternative is to use the scanf engine's white-space-matching
directives, e.g.:

n = sscanf(buffer, "%d\t%*c\b% d", &from, &to); /* odd way to write it */

Here the whitespace in the format -- \t and \b characters, which
is why it is odd since most people would just use a blank -- tells
scanf to read and ignore zero or more white-space characters in
the input (the string in the buffer, in this case).

The second whitespace directive is actually redundant, because %d
skips initial whitespace automatically anyway, so a more minimal
(and less odd) form is:

n = sscanf(buffer, "%d %*c%d", &from, &to);

If you would like to *require* that the single eaten-up character
be a hyphen, you can just put in a literal hyphen:

n = sscanf(buffer, "%d -%d", &from, &to);

The blank after the first "%d" reads and discards any arbitrary
amount of whitespace that follows the first "%d" conversion, the
"-" then matches only a literal '-' character, and the second %d
reads the second number (first reading and discarding any arbitrary
amount of whitespace, as usual). A final, more-symmetrical looking
variant is:

n = sscanf(buffer, "%d - %d", &from, &to);

which redundantly (but harmlessly) asks the scanf engine to skip
whitespace twice.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #3

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

Similar topics

20
2922
by: Sivarn | last post by:
I'm writing a program for that takes dates as input using scanf. I want to verify that the user is inputting a full 4 digits for the year. How do I do this? I know that the return value on printf is the number of printed characters; so if I could somehow get my year variable to store the leading zeros, I could just run a check: int dummy = 0; dummy = printf("%d", year);
33
3170
by: Lalatendu Das | last post by:
Dear friends, I am getting a problem in the code while interacting with a nested Do-while loop It is skipping a scanf () function which it should not. I have written the whole code below. Please help me in finding why such thing is happening and what the remedy to it is. Kindly bear with my English. int main ()
185
17481
by: Martin Jørgensen | last post by:
Hi, Consider: ------------ char stringinput ..bla. bla. bla. do {
14
22004
by: iwinux | last post by:
Hi. Before I use scanf(), I must malloc the memory for it, like this: //Start char * buffer; buffer = malloc(20); scanf("%s", &buffer); //End
3
18824
by: sosij.morris | last post by:
Hi. I'm trying to find some tutorial-type information on using scanf's regular-expression subset for parsing - to no avail so far. I've looked through quite a few posts to clc, but none that have helped me understand what's really allowed, also, the c-faq - when searched for scanf regular (using google site-search) didn't help. Does anyone know of some source - preferably web-based with examples -
14
13814
by: main() | last post by:
I know this is the problem that most newbies get into. #include<stdio.h> int main(void) { char a; scanf("%c",&a); /*1st scanf */ printf("%c\n",a); scanf("%c",&a); /*2nd scanf*/ printf("%c\n",a);
68
4669
by: stasgold | last post by:
Hello. I maybe reinvent the weel ... I'm trying to read positive integer number with the help of scanf, if the input value is not positive number but negaive one zero or char , i have to reread the input until I get the needed pos. number I wrote the code , but it doesn't work the way it should : if i put some char into input, the program goes infinite loop instead of promting me to enter a new value.
9
2692
by: Cao Yi | last post by:
Hi, here's a fract of codes, and what's the line "scanf("%lf%*", &cvi)" doing? ============================= do { printf("\nCoefficient: "); scanf("%lf%*", &cvi); getchar(); } while (cvi <= 0.0);
26
4544
by: tesh.uk | last post by:
Hi Gurus, I have written the following code with the help of Ivor Horton's Beginning C : // Structures, Arrays of Structures. #include "stdafx.h" #include "stdio.h" #define MY_ARRAY 15
13
4378
by: FerrisUML | last post by:
Hello everyone! I new to C and am having the following problem. In the below program, the last scanf is being ignored and the program exits. Can anyone see anything that im doing wrong? Thanks in advance. #include <stdio.h> /* HOMEWORK: Assignment 1 Name: Dennis McQuilken
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10178
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10128
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9971
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9000
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7521
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5406
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3678
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2898
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.