Question about fgets.

Discussion in 'C Programming' started by Tcc, Oct 2, 2004.

  1. Tcc

    Tcc Guest

    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.
     
    Tcc, Oct 2, 2004
    #1
    1. Advertising

  2. Tcc

    Mike Wahler Guest

    "Tcc" <> wrote in message
    news:cjmjfu$2qns$...
    > 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
     
    Mike Wahler, Oct 2, 2004
    #2
    1. Advertising

  3. Tcc

    Malcolm Guest

    "Tcc" <> 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.
     
    Malcolm, Oct 2, 2004
    #3
  4. Tcc

    Dan Pop Guest

    In <UkA7d.864$> "Mike Wahler" <> writes:

    >"Tcc" <> wrote in message
    >news:cjmjfu$2qns$...
    >> 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:
    Currently looking for a job in the European Union
     
    Dan Pop, Oct 4, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. herrcho

    gets, fgets Question

    herrcho, Sep 25, 2003, in forum: C Programming
    Replies:
    11
    Views:
    725
    Dan Pop
    Sep 29, 2003
  2. Walter Dnes (delete the 'z' to get my real address

    fgets()/strlen unix/DOS portability question

    Walter Dnes (delete the 'z' to get my real address, Jun 1, 2004, in forum: C Programming
    Replies:
    3
    Views:
    736
    Walter Dnes (delete the 'z' to get my real address
    Jun 2, 2004
  3. Roman Mashak

    fgets() question

    Roman Mashak, Jun 10, 2005, in forum: C Programming
    Replies:
    3
    Views:
    413
    Malcolm
    Jun 10, 2005
  4. Replies:
    43
    Views:
    1,979
    U S Contractors Offering Service A Non-profit
    Nov 30, 2006
  5. Bill Cunningham

    fgets question

    Bill Cunningham, Jun 9, 2008, in forum: C Programming
    Replies:
    20
    Views:
    776
    fuzhen
    Jun 11, 2008
Loading...

Share This Page