ideas on stream input

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

I want to read formatted text from a text file into a function. Would I
use fscanf or fgetc. For example, I have a text file called "num" that says
the following:

1 12.35
2 11.68

I want to read this text, stored it and pass it as parameters to other
functions. If I used fscanf I have no idea how to use the format specifiers
since I don't really use the scanf family. I usually use fgets to stdin for
input and puts for writing. But this is to a file stream. I hope I'm making
sense. Someone help me out with how reading with fscanf works. That's a new
one on me.

Bill
 
B

Ben Bacarisse

Bill Cunningham said:
I want to read formatted text from a text file into a function. Would I
use fscanf or fgetc. For example, I have a text file called "num" that says
the following:

1 12.35
2 11.68

I want to read this text, stored it and pass it as parameters to other
functions. If I used fscanf I have no idea how to use the format specifiers
since I don't really use the scanf family.

You used to. In 2002 you wrote "I used to use scanf() all the time" and
in 2008 I answered a question from you about fscanf formats.

<snip>
 
B

Bill Cunningham

Ben said:
You used to. In 2002 you wrote "I used to use scanf() all the time"
and in 2008 I answered a question from you about fscanf formats.

<snip>

I'll check and see what I can find.

Bill
 
B

Bill Cunningham

Ben said:
You used to. In 2002 you wrote "I used to use scanf() all the time"
and in 2008 I answered a question from you about fscanf formats.

<snip>

I did use scanf for a while until fgets. That was a long time ago.

Bill
 
N

Nick Keighley

    I want to read formatted text from a text file into a function.

ITYM "...into an array"
Would I
use fscanf or fgetc.

dunno what you'd do. But I'd use fgets() followed by sscanf().
fscanf() is tricky to write the error handling and fgetc() you're
going to have to parse the input yourself. Another option for
converting numbers is strtol() et al.
For example, I have a text file called "num" that says
the following:

1 12.35
2 11.68

I want to read this text, stored it and pass it as parameters to other
functions. If I used fscanf I have no idea how to use the format specifiers
since I don't really use the scanf family.
RTFM

I usually use fgets to stdin for
input and puts for writing. But this is to a file stream.

the "f" in fgets() stands for "file".
I hope I'm making
sense. Someone help me out with how reading with fscanf works. That's a new
one on me.

you too idle to open a book? have you heard of the internet?
 
R

Rui Maciel

Bill said:
I want to read formatted text from a text file into a function. Would
I
use fscanf or fgetc. For example, I have a text file called "num" that
says the following:

1 12.35
2 11.68

I want to read this text, stored it and pass it as parameters to other
functions.

Don't rely on neither. Develop a proper parser and you will end up with a
solution which is far better, far safer, far more flexible and far more
powerful. It will also let you rely on a syntax which is far easier to read
and understand.


Rui Maciel
 
B

Bill Cunningham

superpollo said:
could you please expand this a little'?

bye

Yes. I will write some code and post a little later. Then all can see
what I'm trying to do.

Bill
 
B

Bill Cunningham

Rui said:
Don't rely on neither. Develop a proper parser and you will end up
with a solution which is far better, far safer, far more flexible and
far more powerful. It will also let you rely on a syntax which is
far easier to read and understand.

Sounds great. The thing is I don't know how to write a parser and have
looked online for help. When I have some code together I will post
something.

Bill
 
B

Bill Cunningham

Nick Keighley wrote:

[snip]
the "f" in fgets() stands for "file".

scanf has buffer problems. I use this code...

fgets(variable,sizeof (int),stdin);
 
L

lovecreatesbeauty

you too idle to open a book? have you heard of the internet?

will you still ask the question if bill posted his posts by google
groups?

and i can't understand why bill can't master C even when he himself is
a native english speaker.
 
B

Barry Schwarz

Nick Keighley wrote:

[snip]
the "f" in fgets() stands for "file".

scanf has buffer problems. I use this code...

fgets(variable,sizeof (int),stdin);

It wouldn't surprise me at all if you actually used code like this.

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

Barry Schwarz

Yes. I will write some code and post a little later. Then all can see
what I'm trying to do.

If you want us to see what you are trying to do, describe it in
English. Don't inundate us with your wild guesses at butchered code.
 
N

Nick Keighley

Nick Keighley wrote:

[snip]
the "f" in fgets() stands for "file".

    scanf has buffer problems. I use this code...

note that I said use sscanf(). That has two Ss on the front
fgets(variable,sizeof (int),stdin);

what if your number has more than say 3 characters in it? Will your
code read 1000 correctly? [the answer is "no").
 
M

Michael Angelo Ravera

I want to read formatted text from a text file into a function. Would I
use fscanf or fgetc. For example, I have a text file called "num" that says
the following:

1 12.35
2 11.68

I want to read this text, stored it and pass it as parameters to other
functions. If I used fscanf I have no idea how to use the format specifiers
since I don't really use the scanf family. I usually use fgets to stdin for
input and puts for writing. But this is to a file stream. I hope I'm making
sense. Someone help me out with how reading with fscanf works. That's a new
one on me.

Koko ni Kore wa ikimasu....

Would you describe the format of the file? Is it fixed field, whitespace delimited, or what? Might there be commas? What significance would they have?(do separate fields or are they just for readability of the numbers?) Are there two fields per line or might there be more?

Different methods apply depending upon the answers to those questions.

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 */
}
 
B

Ben Bacarisse

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 */
}

That's a little un-C-like. Because of the way feof works, it's usually
neater write the input operation in the loop condition:

while (fscanf(f, "%d %f", &seq, &val) == 2) {
/* Do something with input */
}

I think it's simpler and more natural (in particular how it handles
end-of-file) but these are minor points. The big win for me is that it
emphasises the normal flow -- what should happen is explicit rather than
being the result of two negatives (!feof(f) and not num_args < 2).

If you need to know what's gone wrong, you can test feof (and/or ferror)
after the loop.

Two details: %5.2f is not a valid scanf format and the \n at the end of
the format adds nothing. It might, though, suggest a false security to
those less well versed in C, so I'd skip it altogether.
 
J

Jorgen Grahn

Rui Maciel ha scritto:

could you please expand this a little'?

First, quote what he wrote properly:

Rui:
I am not him, but I would:

- Define my input language. An example is not a definition.

Is my language line-oriented with positive integer, space,
floating-point value, end of line ? Or would I want to accept more
space around the numbers? Would I want to allow empty lines and
comments?

- Then I'd follow the Perl idiom for writing the parser, but without
the help of regexes.
- read a line at a time
- just whitespace, or a single-line comment #foo? Skip it.
- see if I can find two non-blank parts of the line using isspace()
- on the first, run strtol() or strtoul() and deal with any errors,
including overflow
- on the second, run strtod() similarly

- make sure that in the end the caller can tell the difference between
- no input
- good input
- some good input, then syntax error at line N

When you've done this once or twice, it's not that hard.

/Jorgen
 
P

Philip Lantz

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)];

:)
 
B

Bill Cunningham

Jorgen said:
First, quote what he wrote properly:

Rui:

I am not him, but I would:

- Define my input language. An example is not a definition.

Is my language line-oriented with positive integer, space,
floating-point value, end of line ? Or would I want to accept more
space around the numbers? Would I want to allow empty lines and
comments?

- Then I'd follow the Perl idiom for writing the parser, but without
the help of regexes.
- read a line at a time
- just whitespace, or a single-line comment #foo? Skip it.
- see if I can find two non-blank parts of the line using isspace()
- on the first, run strtol() or strtoul() and deal with any errors,
including overflow
- on the second, run strtod() similarly

- make sure that in the end the caller can tell the difference between
- no input
- good input
- some good input, then syntax error at line N

When you've done this once or twice, it's not that hard.

/Jorgen

I want at 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);

void *tread(void *pointer) //parameter

The function would reach into the text file via a pointer and retreive any
of these 3 fields of data on successive calls and return the void * as a
parameter to pass to another function that will accept the void* as a
parameter. isspace() I am thinking needs to be in there somewhere too.

If this isn't clear please say so. I'm going to try to piece something
together today.

Bill
 
B

Ben Bacarisse

Bill Cunningham said:
I want at 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.

Do you get to choose the file format? If so, you could pick format for
the date this is much easier for C's date and time functions. For
example

1 15.36 2011 09 12
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"). The spaces serve no function other
than to make the format more readable. Dates are likely to exceed the
minimum guaranteed range of "unsigned int" so using "unsigned long"
seems like a better idea.
void *tread(void *pointer) //parameter

The function would reach into the text file via a pointer and retreive any
of these 3 fields of data on successive calls and return the void * as a
parameter to pass to another function that will accept the void* as a
parameter. isspace() I am thinking needs to be in there somewhere too.

If this isn't clear please say so. I'm going to try to piece something
together today.

It's not clear to me, no. I am not sure what "reaching into" means, and
I am puzzled by the term "retrieve any of these 3 fields of data on
successive calls". It sounds like it gets to choose which of the fields
it returns. Furthermore, the function prototype is not helpful. Using
void * just obscures what it's doing and will make using the results
harder.
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top