473,238 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,238 software developers and data experts.

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 1582
don't use scanf()

Dec 8 '05 #2
nick <i1********@yahoo.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_Keith) 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
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...
8
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...
0
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...
5
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...
2
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...
12
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...
1
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...
3
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...
5
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.