Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   ideas on stream input (http://www.velocityreviews.com/forums/t806673-ideas-on-stream-input.html)

Bill Cunningham 12-07-2011 02:14 AM

ideas on stream input
 
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



Ben Bacarisse 12-07-2011 02:28 AM

Re: ideas on stream input
 
"Bill Cunningham" <nospam@nspam.invalid> writes:

> 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>
--
Ben.

Bill Cunningham 12-07-2011 09:40 PM

Re: ideas on stream input
 
Ben Bacarisse wrote:
> "Bill Cunningham" <nospam@nspam.invalid> writes:
>
>> 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>


I'll check and see what I can find.

Bill



Bill Cunningham 12-07-2011 09:41 PM

Re: ideas on stream input
 
Ben Bacarisse wrote:
> "Bill Cunningham" <nospam@nspam.invalid> writes:
>
>> 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>


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

Bill



Nick Keighley 12-08-2011 09:18 AM

Re: ideas on stream input
 
On Dec 7, 2:14*am, "Bill Cunningham" <nos...@nspam.invalid> wrote:

> * * 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?

Rui Maciel 12-08-2011 11:19 AM

Re: ideas on stream input
 
Bill Cunningham wrote:

> 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

Bill Cunningham 12-09-2011 02:26 AM

Re: ideas on stream input
 
superpollo wrote:

> 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



Bill Cunningham 12-09-2011 02:27 AM

Re: ideas on stream input
 
Rui Maciel wrote:

> 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



Bill Cunningham 12-09-2011 02:28 AM

Re: ideas on stream input
 
Nick Keighley wrote:

[snip]

> the "f" in fgets() stands for "file".


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

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

>> 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?




lovecreatesbeauty 12-09-2011 05:20 AM

Re: ideas on stream input
 
On Dec 8, 5:18*pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
> On Dec 7, 2:14*am, "Bill Cunningham" <nos...@nspam.invalid> wrote:
> > I hope I'm making
> > sense. Someone help me out with how reading with fscanf works. That's anew
> > one on me.

>
> 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.


All times are GMT. The time now is 07:44 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.