fscanf equivalent in c++

Discussion in 'C++' started by John Phung, Apr 6, 2004.

  1. John Phung

    John Phung Guest

    Is there a fscanf equivalent for c++? Here's what I'm talking about:

    unsigned int NextAddress(ifstream& AddressFile) {
    unsigned int next_address;

    -->How do I rewrite the fscanf listed below using ifstream?
    -->fscanf(AddressFile, "%x", next_address);

    if(AddressFile.eof()) {
    return 0;
    }
    return next_address;

    } //Return the next address being read from a file in the main program
     
    John Phung, Apr 6, 2004
    #1
    1. Advertising

  2. John Phung

    Rolf Magnus Guest

    John Phung wrote:

    > Is there a fscanf equivalent for c++?


    There is fscanf. ;-)

    > Here's what I'm talking about:
    >
    > unsigned int NextAddress(ifstream& AddressFile) {
    > unsigned int next_address;
    >
    > -->How do I rewrite the fscanf listed below using ifstream?
    > -->fscanf(AddressFile, "%x", next_address);


    AddressFile >> std::hex >> next_address;

    >
    > if(AddressFile.eof()) {
    > return 0;


    What if there is any other special condition than eof?

    > }
    > return next_address;
    >
    > } //Return the next address being read from a file in the main program


    --
    "Time flies like an arrow. Fruit flies like a banana."
    - Groucho Marx
     
    Rolf Magnus, Apr 6, 2004
    #2
    1. Advertising

  3. John Phung wrote:

    > Is there a fscanf equivalent for c++? Here's what I'm talking about:


    The equivalent of fscanf() in C++ is fscanf(). But don't use it.

    >
    > unsigned int NextAddress(ifstream& AddressFile) {
    > unsigned int next_address;
    >
    > -->How do I rewrite the fscanf listed below using ifstream?
    > -->fscanf(AddressFile, "%x", next_address);


    And here's why you shouldn't use it. If you used this code, your program
    would be severely broken. The %x format specifier expects the
    corresponding argument to be of type "pointer to unsigned int". You
    passed the wrong type, and invoked undefined behavior.

    The scanf and printf families of functions don't provide reasonable
    type-checking, and are horribly error-prone and very dangerous to use.
    That's why C++ provides better alternatives in the form of stream classes.

    Anything that puts the burden of type-checking on the programmer should
    be avoided. Such things should be used only when absolutely necessary,
    and then only with great caution by someone who know what they are doing.

    Boost also provides a type-safe library for printf()-like formatting:

    http://www.boost.org/libs/format/index.html

    Although it does not appear to provide anything scanf()-like.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Apr 6, 2004
    #3
  4. "Rolf Magnus" <> wrote in message
    news:c4tse0$dqk$06$-online.com...
    > John Phung wrote:
    >
    > > Is there a fscanf equivalent for c++?

    >
    > There is fscanf. ;-)
    >
    > > Here's what I'm talking about:
    > >
    > > unsigned int NextAddress(ifstream& AddressFile) {
    > > unsigned int next_address;
    > >
    > > -->How do I rewrite the fscanf listed below using ifstream?
    > > -->fscanf(AddressFile, "%x", next_address);

    >
    > AddressFile >> std::hex >> next_address;


    what about

    fscanf(AddressFile, "%i", next_address);

    ???
     
    Nick Hounsome, Apr 7, 2004
    #4
  5. Nick Hounsome wrote:

    > "Rolf Magnus" <> wrote in message
    > news:c4tse0$dqk$06$-online.com...
    >
    >>John Phung wrote:
    >>
    >>>Here's what I'm talking about:
    >>>
    >>>unsigned int NextAddress(ifstream& AddressFile) {
    >>> unsigned int next_address;
    >>>
    >>>-->How do I rewrite the fscanf listed below using ifstream?
    >>>-->fscanf(AddressFile, "%x", next_address);

    >>
    >>AddressFile >> std::hex >> next_address;

    >
    >
    > what about
    >
    > fscanf(AddressFile, "%i", next_address);
    >


    Undefined behavior. %i requires the corresponding argument to be of type
    "pointer to (signed) int". You have provided an argument of type
    "unsigned int".

    As I said in my other reply in this thread:

    The scanf and printf families of functions don't provide reasonable
    type-checking, and are horribly error-prone and very dangerous to use.
    That's why C++ provides better alternatives in the form of stream classes.

    Anything that puts the burden of type-checking on the programmer should
    be avoided. Such things should be used only when absolutely necessary,
    and then only with great caution by someone who know what they are doing.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Apr 7, 2004
    #5
  6. John Phung

    Rolf Magnus Guest

    Nick Hounsome wrote:

    >> > -->How do I rewrite the fscanf listed below using ifstream?

    ^^^^^^^^^^^^^^

    >> > -->fscanf(AddressFile, "%x", next_address);

    >>
    >> AddressFile >> std::hex >> next_address;

    >
    > what about
    >
    > fscanf(AddressFile, "%i", next_address);
    >
    > ???


    The OP wanted to use an ifstream.
     
    Rolf Magnus, Apr 7, 2004
    #6
  7. In message <wjYcc.326$>, Kevin
    Goodsell <> writes
    >Nick Hounsome wrote:
    >
    >> "Rolf Magnus" <> wrote in message
    >> news:c4tse0$dqk$06$-online.com...
    >>
    >>>John Phung wrote:
    >>>
    >>>>Here's what I'm talking about:
    >>>>
    >>>>unsigned int NextAddress(ifstream& AddressFile) {
    >>>> unsigned int next_address;
    >>>>
    >>>>-->How do I rewrite the fscanf listed below using ifstream?
    >>>>-->fscanf(AddressFile, "%x", next_address);
    >>>
    >>>AddressFile >> std::hex >> next_address;

    >> what about
    >> fscanf(AddressFile, "%i", next_address);
    >>

    >
    >Undefined behavior. %i requires the corresponding argument to be of
    >type "pointer to (signed) int". You have provided an argument of type
    >"unsigned int".


    I suspect that's not his point, which might better have been stated as
    "'what's the ifstream equivalent of fscanf(..,. "%i", ...) ?" Note that
    it's %i, not %d.

    Or to put it anther way, "is there something which will read any of the
    strings 012, 10 and 0x0a giving the same result in each case?"

    --
    Richard Herring
     
    Richard Herring, Apr 8, 2004
    #7
  8. Richard Herring wrote:
    >
    > I suspect that's not his point, which might better have been stated as
    > "'what's the ifstream equivalent of fscanf(..,. "%i", ...) ?" Note that
    > it's %i, not %d.
    >
    > Or to put it anther way, "is there something which will read any of the
    > strings 012, 10 and 0x0a giving the same result in each case?"
    >


    Yes, I see what you mean now. Sorry about the misunderstanding.

    Off the top of my head, I don't know of a way to do this with streams
    (short of writing your own code to check the initial character(s)). A
    quick glance at the list of stream manipulators doesn't reveal anything
    that looks likely to accomplish this.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Apr 8, 2004
    #8
  9. John Phung

    P.J. Plauger Guest

    "Kevin Goodsell" <> wrote in message
    news:eek:Mfdc.1832$...

    > Richard Herring wrote:
    > >
    > > I suspect that's not his point, which might better have been stated as
    > > "'what's the ifstream equivalent of fscanf(..,. "%i", ...) ?" Note that
    > > it's %i, not %d.
    > >
    > > Or to put it anther way, "is there something which will read any of the
    > > strings 012, 10 and 0x0a giving the same result in each case?"
    > >

    >
    > Yes, I see what you mean now. Sorry about the misunderstanding.
    >
    > Off the top of my head, I don't know of a way to do this with streams
    > (short of writing your own code to check the initial character(s)). A
    > quick glance at the list of stream manipulators doesn't reveal anything
    > that looks likely to accomplish this.


    Set basefield to zero and you'll convert integer input with %i.

    P.J. Plauger
    Dinkumware, Ltd.
    http://www.dinkumware.com
     
    P.J. Plauger, Apr 8, 2004
    #9
  10. John Phung

    Julie Guest

    "P.J. Plauger" wrote:
    <snip>

    Hey, congrats on your recent DDJ award!
     
    Julie, Apr 8, 2004
    #10
  11. John Phung

    P.J. Plauger Guest

    "Julie" <> wrote in message
    news:...

    > "P.J. Plauger" wrote:
    > <snip>
    >
    > Hey, congrats on your recent DDJ award!


    Thanks. My first, and probably last, stint as a cover model.

    P.J. Plauger
    Dinkumware, Ltd.
    http://www.dinkumware.com
     
    P.J. Plauger, Apr 8, 2004
    #11
  12. "P.J. Plauger" <> wrote in message
    news:pzjdc.17765$...
    > "Kevin Goodsell" <> wrote in message
    > news:eek:Mfdc.1832$...
    >
    > > Richard Herring wrote:
    > > >
    > > > I suspect that's not his point, which might better have been stated as
    > > > "'what's the ifstream equivalent of fscanf(..,. "%i", ...) ?" Note

    that
    > > > it's %i, not %d.
    > > >
    > > > Or to put it anther way, "is there something which will read any of

    the
    > > > strings 012, 10 and 0x0a giving the same result in each case?"
    > > >

    > >
    > > Yes, I see what you mean now. Sorry about the misunderstanding.
    > >
    > > Off the top of my head, I don't know of a way to do this with streams
    > > (short of writing your own code to check the initial character(s)). A
    > > quick glance at the list of stream manipulators doesn't reveal anything
    > > that looks likely to accomplish this.

    >
    > Set basefield to zero and you'll convert integer input with %i.
    >


    I would like you to be right (and you may well be for dinkum) but 27.4.2.1.2
    table 84
    lists the allowable values for basefield and 0 isn't one of them.

    > P.J. Plauger
    > Dinkumware, Ltd.
    > http://www.dinkumware.com
    >
    >
     
    Nick Hounsome, Apr 9, 2004
    #12
    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. cylin
    Replies:
    7
    Views:
    3,854
    Kevin Goodsell
    Sep 19, 2003
  2. Marc Reclaire

    Question about fscanf ..

    Marc Reclaire, Dec 18, 2003, in forum: C++
    Replies:
    2
    Views:
    384
    Karl Heinz Buchegger
    Dec 18, 2003
  3. Fallon

    fscanf question

    Fallon, Jul 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    384
    Giuseppe
    Jul 13, 2003
  4. Chris Torek
    Replies:
    0
    Views:
    398
    Chris Torek
    Jul 14, 2003
  5. David Rubin
    Replies:
    0
    Views:
    447
    David Rubin
    Jul 14, 2003
Loading...

Share This Page