Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question about fgets.

Reply
Thread Tools

Question about fgets.

 
 
Tcc
Guest
Posts: n/a
 
      10-02-2004
Hi all,

Assume there are some data in a file "a.txt":

abc def 11<---------------------data in a.txt

is it possible for me to use "fgets" function to get the string "abc", "def"
and "11" individulely?
and How?

if can't, what function can I use in order to solve this problem?

Thanks.


 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      10-02-2004
"Tcc" <(E-Mail Removed)> wrote in message
news:cjmjfu$2qns$(E-Mail Removed)...
> Hi all,
>
> Assume there are some data in a file "a.txt":
>
> abc def 11<---------------------data in a.txt
>
> is it possible for me to use "fgets" function to get the string "abc",

"def"
> and "11" individulely?


No. By definition, 'fgets()' reads up to a newline character
or end-of-file (or error occurs), whichever comes first.

> and How?
>
> if can't, what function can I use in order to solve this problem?


'fscanf()'

Or use 'fgets()' to read a whole line into a string, then
use 'sscanf()' against that string to extract the individual
strings.

-Mike


 
Reply With Quote
 
 
 
 
Malcolm
Guest
Posts: n/a
 
      10-02-2004

"Tcc" <(E-Mail Removed)> wrote
>
> Assume there are some data in a file "a.txt":
>
> abc def 11<---------------------data in a.txt
>
> is it possible for me to use "fgets" function to get the string "abc",

"def"
> and "11" individulely?
> and How?
>

fgets() isn't very suitable, since it is designed to read whole lines
terminated by a newline.
>
> if can't, what function can I use in order to solve this problem?
>

You can use fscanf(), or you can use fgets() to read the whole line, and
then call strtok() to parse the input.


 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      10-04-2004
In <UkA7d.864$(E-Mail Removed) t> "Mike Wahler" <(E-Mail Removed)> writes:

>"Tcc" <(E-Mail Removed)> wrote in message
>news:cjmjfu$2qns$(E-Mail Removed)...
>> Hi all,
>>
>> Assume there are some data in a file "a.txt":
>>
>> abc def 11<---------------------data in a.txt
>>
>> is it possible for me to use "fgets" function to get the string "abc",

>"def"
>> and "11" individulely?

>
>No. By definition, 'fgets()' reads up to a newline character
>or end-of-file (or error occurs), whichever comes first.
>
>> and How?
>>
>> if can't, what function can I use in order to solve this problem?

>
>'fscanf()'


Right, except that it needs to be *carefully* used (otherwise it is
very easy to lose track of your actual position in the input stream).

>Or use 'fgets()' to read a whole line into a string, then
>use 'sscanf()' against that string to extract the individual
>strings.


Doesn't work very well when the number of items on a line is not known
in advance. If it is, reading the whole line with fscanf and parsing it
with sscanf is the simplest solution.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: http://www.velocityreviews.com/forums/(E-Mail Removed)
Currently looking for a job in the European Union
 
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
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments