Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > ideas on stream input

Reply
Thread Tools

ideas on stream input

 
 
Bill Cunningham
Guest
Posts: n/a
 
      12-07-2011
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


 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      12-07-2011
"Bill Cunningham" <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      12-07-2011
Ben Bacarisse wrote:
> "Bill Cunningham" <(E-Mail Removed)> 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


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      12-07-2011
Ben Bacarisse wrote:
> "Bill Cunningham" <(E-Mail Removed)> 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


 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      12-08-2011
On Dec 7, 2:14*am, "Bill Cunningham" <(E-Mail Removed)> 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?
 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      12-08-2011
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
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      12-09-2011
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


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      12-09-2011
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


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      12-09-2011
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?



 
Reply With Quote
 
lovecreatesbeauty
Guest
Posts: n/a
 
      12-09-2011
On Dec 8, 5:18*pm, Nick Keighley <(E-Mail Removed)>
wrote:
> On Dec 7, 2:14*am, "Bill Cunningham" <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conversion from Input Stream to Output Stream Kashif Ur Rehman Java 2 05-17-2007 07:50 PM
what is the different between byte stream and character stream? dolphin Java 6 03-18-2007 01:58 PM
get stream mode flags from an opened stream Alexander Korsunsky C++ 1 02-17-2007 10:38 AM
How to GET multi-word input from a *file* stream as opposed to a *console* stream? sherifffruitfly@gmail.com C++ 9 04-27-2006 04:14 PM
Doing readline in a thread from a popen4('rsync ...') stream blocks when the stream ends. Rasmusson, Lars Python 1 04-30-2004 08:10 AM



Advertisments