469,328 Members | 1,271 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

How to force fscanf to find only data on a single input line?

Apologies if this is in the FAQ. I looked, but didn't find it.

In a particular program the input read from a file is supposed to be:

+ 100 200 name1
- 101 201 name2

It is parsed by reading the + character, and then sending the
remainder into fscanf() like

count = fscanf(fp,"%d %d %s",&first_int,&second_int,&string);

This works fine unless the input is bogus. In particular, if
"name1" is left off, fscanf happily reads past the EOL of the
first line and comes back with "-" from the second line
stored in the string. Effectively it sees the bogus line as:

+ 100 200 - 101 201 name2

since it makes no distinction between EOL and other white space.
So count is 3 but the wrong characters are stored in string.

What I want is for count to be 2 and string's contents to be
undefined. Is there some magic format specifier that tells fscanf()
not to go past the EOL when looking for data? Sure, it can be done by
reading a whole line into a buffer, and then using sscanf() on that. It
just seems that there should be a way to make fscanf() "line aware".

Possible?

Thanks,

David Mathog
Aug 28 '07
59 5164
Richard Tobin wrote:
Malcolm McLean <re*******@btinternet.comwrote:
>But what happens once you fragment the heap a little, by performing
other allocations?

What do you think happens?

Possibly each reallocation takes a little longer, depending on the
allocator. I suppose it's possible that there's some first-fit
allocator out there where it changes the number of reallocations, but
I very much doubt it. Can you think of a plausible implementation
that makes it not O(N)?
Take a look at nmalloc, for DJGPP. There are several prime points
about it, which is why I wrote it in the first place.

1. Avoiding long searches to combine blocks on free.
2. Avoiding wasting time copying memory on realloc.
3. Rapid resolution of malloc calls.
4. A complete debugging package for the user (added).

All of these have been arranged to have O(1) performance in number
of allocated blocks. That performance, for free, prevents long
hiatuses when releasing large numbers of allocated blocks. The
free action reduces (but does not eliminate) memory blockage due to
fragmentation. The realloc action minimizes CPU time for realloc.
The code is not (can't be) fully portable, but it is close. I have
not found any bugs.

<http://cbfalconer.home.att.net/download/nmalloc.zip>

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 31 '07 #51
[snips]

On Fri, 31 Aug 2007 14:48:31 +0100, Malcolm McLean wrote:
grows exponentially. This is the standard schoolboy question, if you
invested a penny at 10% interest, applied annually, what would it be worth
in 100 years' time? The answer is rather a large sum.
Indeed? Hmm. Should be simple enough to code...

x = 0.01

loop 100 times, x *= 1.10 at each iteration...

result is something less than $140. Hardly what I'd call "rather a large
sum" for a century of investing.
Sep 2 '07 #52
Kelsey Bjarnason <kb********@gmail.comwrites:
[snips]

On Fri, 31 Aug 2007 14:48:31 +0100, Malcolm McLean wrote:
>grows exponentially. This is the standard schoolboy question, if you
invested a penny at 10% interest, applied annually, what would it be worth
in 100 years' time? The answer is rather a large sum.

Indeed? Hmm. Should be simple enough to code...

x = 0.01

loop 100 times, x *= 1.10 at each iteration...

result is something less than $140. Hardly what I'd call "rather a large
sum" for a century of investing.
It is if you start with more than 1 cent. And it is if, for you, a penny
is a lot of money.

Sep 2 '07 #53
Kelsey Bjarnason wrote:
[snips]

On Fri, 31 Aug 2007 14:48:31 +0100, Malcolm McLean wrote:
>grows exponentially. This is the standard schoolboy question, if you
invested a penny at 10% interest, applied annually, what would it be worth
in 100 years' time? The answer is rather a large sum.

Indeed? Hmm. Should be simple enough to code...

x = 0.01

loop 100 times, x *= 1.10 at each iteration...

result is something less than $140. Hardly what I'd call "rather a large
sum" for a century of investing.
But imagine you were able to invest $100. The wonder of compound
interest at 10% would yield $1,378,061.23 or so.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Sep 2 '07 #54
On Sun, 02 Sep 2007 19:07:37 +0200, Richard wrote:
Kelsey Bjarnason <kb********@gmail.comwrites:
>[snips]

On Fri, 31 Aug 2007 14:48:31 +0100, Malcolm McLean wrote:
>>grows exponentially. This is the standard schoolboy question, if you
invested a penny at 10% interest, applied annually, what would it be worth
in 100 years' time? The answer is rather a large sum.

Indeed? Hmm. Should be simple enough to code...

x = 0.01

loop 100 times, x *= 1.10 at each iteration...

result is something less than $140. Hardly what I'd call "rather a large
sum" for a century of investing.

It is if you start with more than 1 cent.
Which you don't, as the statement was about, I quote, "a penny". Not a C
note. Not a million. A penny.
Sep 2 '07 #55
Kelsey Bjarnason <kb********@gmail.comwrites:
On Sun, 02 Sep 2007 19:07:37 +0200, Richard wrote:
>Kelsey Bjarnason <kb********@gmail.comwrites:
>>[snips]

On Fri, 31 Aug 2007 14:48:31 +0100, Malcolm McLean wrote:

grows exponentially. This is the standard schoolboy question, if you
invested a penny at 10% interest, applied annually, what would it be worth
in 100 years' time? The answer is rather a large sum.

Indeed? Hmm. Should be simple enough to code...

x = 0.01

loop 100 times, x *= 1.10 at each iteration...

result is something less than $140. Hardly what I'd call "rather a large
sum" for a century of investing.

It is if you start with more than 1 cent.

Which you don't, as the statement was about, I quote, "a penny". Not a C
note. Not a million. A penny.
Are you as pedantic and petty in real life?

Why did you snip my reply?

The bit which suggests that 140 quid is a lot to a little boy who thinks
investing one penny is a big thing?
Sep 2 '07 #56
Richard wrote:
Kelsey Bjarnason <kb********@gmail.comwrites:
>On Sun, 02 Sep 2007 19:07:37 +0200, Richard wrote:
>>Kelsey Bjarnason <kb********@gmail.comwrites:

[snips]

On Fri, 31 Aug 2007 14:48:31 +0100, Malcolm McLean wrote:

grows exponentially. This is the standard schoolboy question, if you
invested a penny at 10% interest, applied annually, what would it be worth
in 100 years' time? The answer is rather a large sum.
Indeed? Hmm. Should be simple enough to code...

x = 0.01

loop 100 times, x *= 1.10 at each iteration...

result is something less than $140. Hardly what I'd call "rather a large
sum" for a century of investing.
It is if you start with more than 1 cent.
Which you don't, as the statement was about, I quote, "a penny". Not a C
note. Not a million. A penny.

Are you as pedantic and petty in real life?

Why did you snip my reply?

The bit which suggests that 140 quid is a lot to a little boy who thinks
investing one penny is a big thing?
I see some confusion among cent, the 100th part of a US dollar and
penny, the 240th part of an old UK pound. Remember? Twelve pence to a
shilling, twenty shillings to a pound? The 100th part of the new pound
suffers the name of pee.

I once bought a pint of bitter in a Croyden pub for one and six.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Sep 2 '07 #57
Joe Wright said:
Richard wrote:
<snip>
>The bit which suggests that 140 quid is a lot to a little boy who
thinks investing one penny is a big thing?

I see some confusion among cent, the 100th part of a US dollar and
penny, the 240th part of an old UK pound. Remember? Twelve pence to a
shilling, twenty shillings to a pound? The 100th part of the new pound
suffers the name of pee.
If you assume proper pennies, the answer reduces to just under 57.42.

Any child capable of performing a 100-year compound interest calculation
is also very likely to be capable of realising that it is a
hypothetical exercise, for at least one of several reasons:

(a) very, very few people old enough to open a bank account (or to have
one opened for them) will still be alive 100 years thereafter;
(b) because of the exigencies and vicissitudes of normal life, the money
is very unlikely to remain in the account, unscathed, for an entire
century;
(c) finding a low-risk high-yield account is difficult - getting a 10%
interest rate is very, very difficult indeed - you'd be hard-pressed to
get that rate with a principal of thousands of pounds, let alone with a
single penny.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 3 '07 #58
Richard Heathfield <rj*@see.sig.invalidwrites:
Joe Wright said:
>Richard wrote:

<snip>
>>The bit which suggests that 140 quid is a lot to a little boy who
thinks investing one penny is a big thing?

I see some confusion among cent, the 100th part of a US dollar and
penny, the 240th part of an old UK pound. Remember? Twelve pence to a
shilling, twenty shillings to a pound? The 100th part of the new pound
suffers the name of pee.

If you assume proper pennies, the answer reduces to just under 57.42.

Any child capable of performing a 100-year compound interest calculation
is also very likely to be capable of realising that it is a
hypothetical exercise, for at least one of several reasons:

(a) very, very few people old enough to open a bank account (or to have
one opened for them) will still be alive 100 years thereafter;
(b) because of the exigencies and vicissitudes of normal life, the money
is very unlikely to remain in the account, unscathed, for an entire
century;
(c) finding a low-risk high-yield account is difficult - getting a 10%
interest rate is very, very difficult indeed - you'd be hard-pressed to
get that rate with a principal of thousands of pounds, let alone with a
single penny.
Off Topic. Please take it to a newsgroup which discusses bank
accounts. We do not discuss this stuff here.

blah blah blah blah
Sep 3 '07 #59
[snips]

On Mon, 03 Sep 2007 00:46:55 +0200, Richard wrote:
>Which you don't, as the statement was about, I quote, "a penny". Not a C
note. Not a million. A penny.

Are you as pedantic and petty in real life?
I try to be, yes, when examining claims of this sort.
Why did you snip my reply?
Because it added nothing relevant.
The bit which suggests that 140 quid is a lot to a little boy who thinks
investing one penny is a big thing?
A 100+ year old little boy? Indeed. Or do you mean Great Grandpa should
invest a penny so the kid three generations down the line can inherit a
hair over a C note? Not a very smart old coot, was he?
Sep 3 '07 #60

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Kay | last post: by
4 posts views Thread by Cal Lidderdale | last post: by
37 posts views Thread by PeterOut | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.