ideas on stream input

  • Thread starter Bill Cunningham
  • Start date
N

Nick Keighley

    I want [to read a] text file in this format.
1 15.36 120912
1 is line one, 15.36 a securities price, 120912 or I guess it should be11
is the date.
I am thinking fscanf(fp,"%u%.2f%d\n",&a,&b,&c);

The \n does not help -- it almost certainly does not do what you think
it does -- and %.2f is not a valid format.  Why use %u for the line
number and %d for the date?

I'd use fscanf(fp, "%u %f %lu").

replace %f with %lf maybe? And remember to check the return value of
fscanf()

<snip>
 
B

Ben Bacarisse

Nick Keighley said:
    I want [to read a] text file in this format.
1 15.36 120912
1 is line one, 15.36 a securities price, 120912 or I guess it should be 11
is the date.
I am thinking fscanf(fp,"%u%.2f%d\n",&a,&b,&c);

The \n does not help -- it almost certainly does not do what you think
it does -- and %.2f is not a valid format.  Why use %u for the line
number and %d for the date?

I'd use fscanf(fp, "%u %f %lu").

replace %f with %lf maybe?

Yes, maybe. I did not want to change this too much. When using double
for all floating point calculation, %lf has the advantage that you can
use it for both scanf and printf.
And remember to check the return value of
fscanf()

Of course -- that's why I removed Bill's ; when I suggested my format!
 
S

Seebs

"Bill Cunningham <nospam@invalid>"

Please don't change your posting email address. I can't tell whether you're
genuinely that incompetent, or just trolling, but the fact is, your posts are
pretty much pure noise, you don't appear to be responding at all to anything
anyone says, you claim to have figured things out and then get them wrong
again a week later, and you are *functionally* just trolling. It is useful
to people if you keep your email address the same so they can keep you
killfiled.

-s
 
B

Barry Schwarz

However, for novices following this thread, be aware that this code
can never produce the correct results.

Never is such a strong word. What if it were preceded by
char variable[sizeof (int)];

If int is 4 bytes, then a user typing in 1000 which is guaranteed to
be a valid int would get the wrong result.

And since in this thread the OP is attempting to extract an int and
not a string, your question is a non-sequitur.
 
K

Keith Thompson

Michael Angelo Ravera said:
If all of the lines in the file look like what you have shown, you
could use a code fragment soething like this (ignoring errors):

int seq;
float val;
FILE * f;
int num_args;

if (!(f = fopen ("num", "r"))
giveup;
while (!feof (f)) do
{
num_args = fscanf (f, "%d %5.2f\n", & seq, & val);
if (num_args < 2)
giveup;
/* Do something with input */
}

(Ignoring errors, including the extraneous "do", as you requested.)

The "while (!feof (f))" is a red flag indicating that you're doing it
wrong.

Input loops should terminate when the input function returns a result
indicating that it can read no more input, not when feof() returns true.
For one thing, feof() becomes true only *after* an input operation has
failed. For another, an input error will cause ferror(), but *not*
feof(), to become true.

feof() and ferror() are for determining *why* there's no more input.
 
B

Bill Cunningham

Seebs said:
"Bill Cunningham <nospam@invalid>"

Please don't change your posting email address. I can't tell whether
you're genuinely that incompetent, or just trolling, but the fact is,
your posts are pretty much pure noise, you don't appear to be
responding at all to anything anyone says, you claim to have figured
things out and then get them wrong again a week later, and you are
*functionally* just trolling. It is useful to people if you keep
your email address the same so they can keep you killfiled.

My computer crashed and everything had to be reinstalled and I inserted
by mistake a wrong email. Other than the one I've been using. Thank goodness
for carbonite.

Bill
 
B

Bill Cunningham

Nick Keighley wrote:

[snip]
replace %f with %lf maybe? And remember to check the return value of
fscanf()

That return value of fscanf. It's a little different than most functions
I've worked with. Isn't that where the $n[ comes into play? To check every
pointer individually?

Bill
 
P

Philip Lantz

On Thu, 8 Dec 2011, Bill Cunningham wrote:
fgets(variable,sizeof (int),stdin);

However, for novices following this thread, be aware that this code
can never produce the correct results.

Never is such a strong word. What if it were preceded by
char variable[sizeof (int)];

If int is 4 bytes, then a user typing in 1000 which is guaranteed to
be a valid int would get the wrong result.

And since in this thread the OP is attempting to extract an int and
not a string, your question is a non-sequitur.

Oh, I hope I didn't imply that this would be a suitable way to read
an int. I agree, in the context of this thread, this is undoubtedly
useless. However, your use of the word "never" (with very little
context) attracted my attention.

Philip
 
B

Bill Cunningham

Philip said:
However, for novices following this thread, be aware that this code
can never produce the correct results.

Never is such a strong word. What if it were preceded by
char variable[sizeof (int)];

:)

I do also if I'm wanting to enter a word of 4 chars I type

fgets(variable,4,stdin)
It's better than using gets(variable);

Bill
 
B

Ben Bacarisse

Bill Cunningham said:
Philip said:
On Thu, 8 Dec 2011, Bill Cunningham wrote:
fgets(variable,sizeof (int),stdin);

However, for novices following this thread, be aware that this code
can never produce the correct results.

Never is such a strong word. What if it were preceded by
char variable[sizeof (int)];

:)

I do also if I'm wanting to enter a word of 4 chars I type

fgets(variable,4,stdin)
It's better than using gets(variable);

That reads no more than three characters. There's no sense in which
they will form a word -- you just get the next three characters (fewer
if a newline is found first). variable should be an expression that
points to a char array.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top