473,765 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to check no. of spaces were skipped?

i use sscanf()to get the words in a line, it will skip all the space
automatically, if i want to know how many spaces were skipped and get
the words in a line, what can i do?
thanks!
Dec 8 '05 #1
8 1610
don't use scanf()

Dec 8 '05 #2
nick <i1********@yah oo.com> wrote:
i use sscanf()to get the words in a line, it will skip all the space
automatically, if i want to know how many spaces were skipped and get
the words in a line, what can i do?
thanks!


Don't use sscanf()[1]. Use strcspn() instead.

Richard

[1] In this case. Out of *scanf(), it is the most useful generally.
Dec 8 '05 #3
what's the difference between rand() & random() ?

thanx!
i am the new comer here,maybe i break some rules that i don't kown
before;please forgive me...
Dec 8 '05 #4
On 2005-12-08, sonic0568 <so*******@sina .com> wrote:
what's the difference between rand() & random() ?
The latter is not part of the C standard, and evidently originated with
4.2 BSD.
thanx! i am the new comer here,maybe i break some rules that i don't
kown before; please forgive me...

Dec 8 '05 #5
nick wrote:
i use sscanf()to get the words in a line, it will skip all the space
automatically, if i want to know how many spaces were skipped and get
the words in a line, what can i do?


sscanf() is not the best tool for the job, as others
have mentioned. But if you have some kind of passionate
desire to use sscanf(), look up the "%n" specifier.

--
Eric Sosman
es*****@acm-dot-org.invalid
Dec 8 '05 #6
nick wrote:
i use sscanf()to get the words in a line, it will skip all the space
automatically, if i want to know how many spaces were skipped and get
the words in a line, what can i do?
thanks!


You can modify your format string to explicitly skip the whitespace
first, then use the %n conversion specifier to determine how much space
was skipped. The %n conversion stored the number of characters
consumed so far into the specified int. For example, if your original
call looked like this (buffer size and error checking removed for
clarity):

char buf[100];
scanf("%s", buf);

which would skip leading whitespace and put the first word into buf,
you could change it to:

char buf[100];
int whitespace;
scanf(" %n%s", &whitespace, buf);

and the number of whitespace characters skipped would be stored into
"whitespace ".

If you only want to skip space characters or some other specific set of
characters, you can also use this technique with an
assignment-suppressing character set specifier:

char buf[100];
int skipped;
scanf("%*[ _]%n%s", &skipped, buf);

The above example will skip all leading space and underscore
characters, store the number of skipped characters into "skipped", and
read the next word into buf. Note that the %s conversion specifier may
skip additional whitespace characters besides the space character so
this example would not catch those characters or space/underscore
characters following them.

You can also use this technique in the middle of a large format string
as well, although it requires double the effort and some subtraction:

scanf("...%s%n %n%s...", ... buf1, val1, val2, buf2 ...);
The amount of whitespace skipped before the second %s conversion is
then val2-val1.

Robert Gamble

Dec 8 '05 #7
Robert Gamble wrote:
nick wrote:
i use sscanf()to get the words in a line, it will skip all the space
automatically , if i want to know how many spaces were skipped and get
the words in a line, what can i do?
thanks!

You can modify your format string to explicitly skip the whitespace
first, then use the %n conversion specifier to determine how much space
was skipped. The %n conversion stored the number of characters
consumed so far into the specified int. For example, if your original
call looked like this (buffer size and error checking removed for
clarity):

char buf[100];
scanf("%s", buf);

which would skip leading whitespace and put the first word into buf,
you could change it to:

char buf[100];
int whitespace;
scanf(" %n%s", &whitespace, buf);

and the number of whitespace characters skipped would be stored into
"whitespace ".

If you only want to skip space characters or some other specific set of
characters, you can also use this technique with an
assignment-suppressing character set specifier:

char buf[100];
int skipped;
scanf("%*[ _]%n%s", &skipped, buf);

The above example will skip all leading space and underscore
characters, store the number of skipped characters into "skipped", and
read the next word into buf. Note that the %s conversion specifier may
skip additional whitespace characters besides the space character so
this example would not catch those characters or space/underscore
characters following them.

You can also use this technique in the middle of a large format string
as well, although it requires double the effort and some subtraction:

scanf("...%s%n %n%s...", ... buf1, val1, val2, buf2 ...);
The amount of whitespace skipped before the second %s conversion is
then val2-val1.

Robert Gamble

thanks
Dec 8 '05 #8
"sonic0568" <so*******@sina .com> writes:
what's the difference between rand() & random() ?
Jordan already answered that.
i am the new comer here,maybe i break some rules that i don't kown
before;please forgive me...


A new question should start a new thread rather than being a followup
to an existing one.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 8 '05 #9

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

Similar topics

6
4296
by: Ruben | last post by:
Hello. I am trying to read a small text file using the readline statement. I can only read the first 2 records from the file. It stops at the blank lines or at lines with only spaces. I have a while statement checking for an empty string "" which I understand represents an EOF in Python. The text file has some blank lines with spaces and other with blanks. Thanks a lot.
8
1825
by: Remy Blank | last post by:
Hello unittest users, In a project I am working on, I have a series of tests that have to be run as root, and others as a normal user. One solution is to separate the tests into two different files, and run the right one. I don't like this too much, as I prefer to group tests by "function". Another solution is to build the test suite differently, depending
0
2054
by: Remy Blank | last post by:
Ok, here we go. I added the possibility for tests using the unittest.py framework to be skipped. Basically, I added two methods to TestCase: TestCase.skip(msg): skips unconditionally TestCase.skipIf(expr, msg): skips if expr is true These can be called either in setUp() or in the test methods. I also added reporting of skipped tests to TestResult, _TextTestResult and
5
6316
by: Steve Wylie | last post by:
I am constructing an HTML questionnaire and one of the questions requires people to rate some choices from 1 to 5, where 1 is their favourite and 5 is their least favourite: Car Bus Taxi cab Train Airplane
2
1716
by: Ed Eichman | last post by:
Hi Guys, When I create an ASP.NET web app, which files do I need to check into my version control? I assume that the "bin" directory is skipped, but I'm not sure about the hidden directories (_vti_cnf, _vti_pvt, _vti_script, and _vti_txt). What are these things, and which of their contents do I have to check in? NOTE: I know at least some of the hidden dirs' files have to be kept, because trashing them caused problems in opening the...
12
14536
by: tshad | last post by:
What would be a good way to check programmatically whether a service was running? We have a service that dies periodically and I need to check to see if this service is running. I know how to check to see if the status is in stopped or running mode. But that doesn't tell me if it is actually running. I need to know this so that if it happens I can programmatically start the same service on another machine.
1
5959
by: huyuhui | last post by:
The following is a question of LOAD utility. Question: How does the DB2 enforce table check constraints for data added to table with the LOAD utility? A. With the BUILD phase of LOAD B. With the SET INTEGRITY statement C. With the DELETE phase of the LOAD D. With the UPDATE CONSTRAINTS statement Answer is A
3
12800
by: stathisgotsis | last post by:
Hello everyone, Trusting K&R2 i thought until recently that spaces are ignored in scanf's format string. Reading arguments to the contrary confused me a little. So i now ask: Is scanf("%d%d",...) different from scanf("%d %d",...) in the Standard's point of view? Thank you.
5
4070
by: Sudhir123 | last post by:
Hi.. I need to find out whether user has entered only black spaces or some text which might have some spaces.. But i more interested to check for blanck spaces (by spacebar key). how i will find out this. I used trim function, but it trims text also..i m using my own framework which i built for application. Plz suggest me somthing.. following functions are not working. element.length==0 and element==null
0
9568
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
9399
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9957
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
8832
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
7379
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
5276
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...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2806
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.