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

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 5520
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Kay | last post by:
1) If i want to read data from a txt file, eg John; 23; a Mary; 16; i How can I read the above data stopping reading b4 each semi-colon and save it in three different variables ? 2) If I...
4
by: Cal Lidderdale | last post by:
My input line is i1,i2,i3,i4,i5,i6,i7,i8^,...i596,597, ... 14101,14102...NL/CR very long line of data - I only want the first 8 items and the delimiter between 8 & 9 is a carrot "^". The line...
4
by: John | last post by:
I need to read data from the file like the following with name and score, but some line may only has name without score: joe 100 amy 80 may Here's my code, but it couldn't read the line with...
37
by: PeterOut | last post by:
I am using MS Visual C++ 6.0 on Windows XP 5.1 (SP2). I am not sure if this is a C, C++ or MS issue but fscanf has been randomly hanging on me. I make the call hundreds, if not thousands, of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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...

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.