473,322 Members | 1,409 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,322 software developers and data experts.

scanf question using %[^$]

I'm trying to use scanf() to get a string that is terminated by a
$sign:

Reading a file line that has: account number, name (terminated by
$sign) and six numbers:
000001 John Doe$ 4 5 6 7 8 9
I'm trying to read it in like so:
int ret, acct, c1, c2, c3, c4, c5, c6;
char name[21];

ret = scanf("%d %[^$] %d %d %d %d %d %d",
&acct, name, &c1, &c2, &c3, &c4, &c5, &c6);
I would think that (ret should == 8), but it's only 2.

Variables acct & name look okay, but the cN vars look like they haven't
been assigne anything.

What am I doing wrong? If I split the name into two seperate strings,
say, first and last names, it works good, but I may not have both a
first & last name on every line.

Aug 22 '06 #1
8 2170
In article <11**********************@p79g2000cwp.googlegroups .com>,
john <br****@yahoo.comwrote:
>000001 John Doe$ 4 5 6 7 8 9
>ret = scanf("%d %[^$] %d %d %d %d %d %d",
%[^$] leaves the $ in the input stream. The space you have after
that in the format does not match the $ in the input stream, so
the format conversion terminates.

Try a format of "%d %[^$]$%d%d%d%d%d%d"
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Aug 22 '06 #2


john wrote On 08/22/06 13:38,:
I'm trying to use scanf() to get a string that is terminated by a
$sign:

Reading a file line that has: account number, name (terminated by
$sign) and six numbers:
000001 John Doe$ 4 5 6 7 8 9
I'm trying to read it in like so:
int ret, acct, c1, c2, c3, c4, c5, c6;
char name[21];

ret = scanf("%d %[^$] %d %d %d %d %d %d",
&acct, name, &c1, &c2, &c3, &c4, &c5, &c6);
I would think that (ret should == 8), but it's only 2.

Variables acct & name look okay, but the cN vars look like they haven't
been assigne anything.

What am I doing wrong? If I split the name into two seperate strings,
say, first and last names, it works good, but I may not have both a
first & last name on every line.
The "%[^$]" stops when it encounters the '$' character,
leaving the '$' unconsumed. Try "%[^$]$". Better yet, try
"%20[^$]$" (to defend against overlength names).

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

Aug 22 '06 #3
john wrote:
I'm trying to use scanf() to get a string that is terminated by a
$sign:

Reading a file line that has: account number, name (terminated by
$sign) and six numbers:
000001 John Doe$ 4 5 6 7 8 9
I'm trying to read it in like so:
int ret, acct, c1, c2, c3, c4, c5, c6;
char name[21];

ret = scanf("%d %[^$] %d %d %d %d %d %d",
&acct, name, &c1, &c2, &c3, &c4, &c5, &c6);
I would think that (ret should == 8), but it's only 2.

Variables acct & name look okay, but the cN vars look like they haven't
been assigne anything.

What am I doing wrong?
Note the difference in the format strings below:

#include <stdio.h>

int main(void)
{
char input[] = "000001 John Doe$ 4 5 6 7 8 9";
int ret, acct, c1, c2, c3, c4, c5, c6;
char name[21];

printf("Attempt to read variables with format\n"
"\"%%d %%[^$] %%d %%d %%d %%d %%d %%d\"\n");
ret = sscanf(input, "%d %[^$] %d %d %d %d %d %d",
&acct, name, &c1, &c2, &c3, &c4, &c5, &c6);
printf("return value from scanf = %d\n\n", ret);

printf("Attempt to read variables with format\n"
"\"%%d %%[^$]$ %%d %%d %%d %%d %%d %%d\"\n");
ret = sscanf(input, "%d %[^$]$ %d %d %d %d %d %d",
&acct, name, &c1, &c2, &c3, &c4, &c5, &c6);
printf("return value from scanf = %d\n\n", ret);
return 0;
}

[output]
Attempt to read variables with format
"%d %[^$] %d %d %d %d %d %d"
return value from scanf = 2

Attempt to read variables with format
"%d %[^$]$ %d %d %d %d %d %d"
return value from scanf = 8
Aug 22 '06 #4
On Tue, 22 Aug 2006 17:38:12 UTC, "john" <br****@yahoo.comwrote:
I'm trying to use scanf() to get a string that is terminated by a
$sign:

Reading a file line that has: account number, name (terminated by
$sign) and six numbers:
000001 John Doe$ 4 5 6 7 8 9
I'm trying to read it in like so:
int ret, acct, c1, c2, c3, c4, c5, c6;
char name[21];

ret = scanf("%d %[^$] %d %d %d %d %d %d",
&acct, name, &c1, &c2, &c3, &c4, &c5, &c6);
Look how scanf works:

%d reads the int.
%d reads until it dedects a whitespace, so it stops reading after John
is readed.
Then it tries to read and eat the '$' but there is none, so it fails
and return 2 as it has filled up 2 variables.
I would think that (ret should == 8), but it's only 2.
No wounder. The format does not match the data coming in.
Variables acct & name look okay, but the cN vars look like they haven't
been assigne anything.

What am I doing wrong? If I split the name into two seperate strings,
say, first and last names, it works good, but I may not have both a
first & last name on every line.
As you reads from stdin and that will mostenly (not always!) assigned
to console (e.g. keyboard) there are lots of lots to get unwanted
data. I would write my own little parser by reading with getc() and
looking at each char if the data coming in is what I want and if not
throwing away the rest of the line (fetc() until newline and printout
an error message to show the user the point he has mistyped.

The C I/O library was designed and written at the tme where punch
cards were the most common input devise and typing something in was
absolutely uncommon. So whenever you have to read some more complex
data you're on your own. It is impossible for scanf() to dedect each
and any typing error a user can made.

Beside that reading simple with %s is an invitation for buffer
overflow, %d can lead to loosing significant digits or if the value is
negative undefined behavior.

However when you have to read text that contains an unknown nuber of
words you lost with scvanf anyway. Even (f)gets() is no solution
because the standard says nothing how many characters are min maxim
can be a simple text line. Buffer overflow can occure.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Aug 22 '06 #5


Herbert Rosenau wrote On 08/22/06 16:03,:
On Tue, 22 Aug 2006 17:38:12 UTC, "john" <br****@yahoo.comwrote:

>>I'm trying to use scanf() to get a string that is terminated by a
$sign:

Reading a file line that has: account number, name (terminated by
$sign) and six numbers:
000001 John Doe$ 4 5 6 7 8 9
I'm trying to read it in like so:
int ret, acct, c1, c2, c3, c4, c5, c6;
char name[21];

ret = scanf("%d %[^$] %d %d %d %d %d %d",
&acct, name, &c1, &c2, &c3, &c4, &c5, &c6);


Look how scanf works:

%d reads the int.
Fine so far.
%d reads until it dedects a whitespace, so it stops reading after John
is readed.
Now you've lost me. What "%d" do you refer to? If it's
the first one, then it stops converting the int when it reaches
the white space, but it does not consume the white space and
certainly doesn't consume "John". If you actually meant to
write "%[^$]" instead of "%d", then it will not stop on white
space at all: it will read all the way through "John Doe" and
stop just before the "$".
Then it tries to read and eat the '$' but there is none, so it fails
and return 2 as it has filled up 2 variables.
This cannot be right. Nothing in the format string tries
to "read and eat the '$'". (And that's the cause of the O.P.'s
difficulty.)

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

Aug 22 '06 #6
Herbert Rosenau wrote:
On Tue, 22 Aug 2006 17:38:12 UTC, "john" <br****@yahoo.comwrote:
<snip>
The C I/O library was designed and written at the tme where punch
cards were the most common input devise and typing something in was
absolutely uncommon.
really? Are you sure about this?
So whenever you have to read some more complex
data you're on your own. It is impossible for scanf() to dedect each
and any typing error a user can made.
<snip>

--
Nick Keighley

Aug 23 '06 #7
In article <11**********************@75g2000cwc.googlegroups. com>,
Nick Keighley <ni******************@hotmail.comwrote:
>The C I/O library was designed and written at the tme where punch
cards were the most common input devise and typing something in was
absolutely uncommon.
>really? Are you sure about this?
It's probably true that most computer input was from punched cards at
that time, but that's not really relevant since C's standard i/o
library was written for Unix, which was designed as an interactive
system.

-- Richard
Aug 23 '06 #8
On Tue, 22 Aug 2006 18:12:59 +0000 (UTC), ro******@ibd.nrc-cnrc.gc.ca
(Walter Roberson) wrote:
In article <11**********************@p79g2000cwp.googlegroups .com>,
john <br****@yahoo.comwrote:
000001 John Doe$ 4 5 6 7 8 9
ret = scanf("%d %[^$] %d %d %d %d %d %d",

%[^$] leaves the $ in the input stream. The space you have after
Right.
that in the format does not match the $ in the input stream, so
the format conversion terminates.
Wrong. %-specifiers in *scanf must match something (at least one input
char) or they fail and terminate the call, but (any) whitespace
matches zero or more (of any) whitespace and continues on its merry
way even if there was no (aka zero) whitespace matched (and used).

It is the second %d that tries and fails to match the still waiting $.
Try a format of "%d %[^$]$%d%d%d%d%d%d"
Right.

- David.Thompson1 at worldnet.att.net
Sep 4 '06 #9

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

Similar topics

12
by: B Thomas | last post by:
Hi, I was reading O'Reilly's "Practical C programming" book and it warns against the use of scanf, suggesting to avoid using it completely . Instead it recomends to use using fgets and sscanf....
20
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...
14
by: Peter Mount | last post by:
Hello I'm having trouble with " scanf("%c", &answer);" on line 20 below. When I run the program in cygwin on Windows 98SE it skips that line completely and ends the program. Does scanf have...
7
by: hugo27 | last post by:
obrhy8 June 18, 2004 Most compilers define EOF as -1. I'm just putting my toes in the water with a student's model named Miracle C. The ..h documentation of this compiler does state that when...
6
by: Pete | last post by:
Following is a fragment from a program I'm writing. I am trying to enter an 8 bit binary representation from keyboard which is then scanfed into &Ptext and then again into &Key1. This is not binary...
4
by: sushant | last post by:
hi why do we use '&' operator in scanf like scanf("%d", &x); but why not in printf() like printf("%d" , x); thnx in advance sushant
17
by: Lefty Bigfoot | last post by:
Hello, I am aware that a lot of people are wary of using scanf, because doing it improperly can be dangerous. I have tried to find a good tutorial on all the ins and outs of scanf() but been...
33
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...
3
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...
3
by: Tinku | last post by:
#include<stdio.h> main() { char line; scanf("%", line); printf("%s", line); } it will read and print the line but what is "%" in general we gives %s, %c .
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...
1
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.