using fscanf to read data for a class member ...

Discussion in 'C++' started by blacksoil@gmail.com, Sep 5, 2006.

  1. Guest

    Hi,

    I have a question regarding reading data from a file and put it to a
    member of a class. I use fscanf.

    The class looks like this

    class myclass
    {
    double a;
    ...
    }

    Now in one of its member function, I want to do the following:

    fscanf(fp,"%f\n",&myclass.a);

    However, it turns out that myclass.a is not given the data.

    I am not familiar with syntax in c++ and could anyone give me some
    suggestions? Thanks a lot!

    --Zhi
    , Sep 5, 2006
    #1
    1. Advertising

  2. mlimber Guest

    wrote:
    > Hi,
    >
    > I have a question regarding reading data from a file and put it to a
    > member of a class. I use fscanf.
    >
    > The class looks like this
    >
    > class myclass
    > {
    > double a;
    > ...
    > }
    >
    > Now in one of its member function, I want to do the following:
    >
    > fscanf(fp,"%f\n",&myclass.a);


    Inside a member function, you would do this:

    fscanf(fp,"%lf\n",&a); // Note the 'l'

    >
    > However, it turns out that myclass.a is not given the data.
    >
    > I am not familiar with syntax in c++ and could anyone give me some
    > suggestions? Thanks a lot!


    In C++, iostreams are generally preferred to C-style I/O because they
    are type safe (e.g., you wouldn't be able omit the 'l' without doing so
    intentionally with a nasty cast) and because you can overload the <<
    and >> operators for your own classes. Look up how to use
    std::ifstream. See also these FAQs:

    http://www.parashift.com/c -faq-lite/input-output.html

    Cheers! --M
    mlimber, Sep 5, 2006
    #2
    1. Advertising

  3. wrote:
    > I have a question regarding reading data from a file and put it to a
    > member of a class. I use fscanf.
    >
    > The class looks like this
    >
    > class myclass
    > {
    > double a;
    > ...
    > }
    >
    > Now in one of its member function, I want to do the following:
    >
    > fscanf(fp,"%f\n",&myclass.a);
    >
    > However, it turns out that myclass.a is not given the data.
    >
    > I am not familiar with syntax in c++ and could anyone give me some
    > suggestions? Thanks a lot!


    RTFM. To convert a 'double' using 'scanf' (and its relatives), you
    need to use '%lf' format.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Sep 5, 2006
    #3
  4. Bart Guest

    wrote:
    <snip>
    > fscanf(fp,"%f\n",&myclass.a);
    >
    > However, it turns out that myclass.a is not given the data.


    Why not use iostreams instead?

    myfile >> myclass;

    For this to work you'll have to overload the >> for your class and open
    a fstream of some sort.

    Regards,
    Bart.
    Bart, Sep 5, 2006
    #4
  5. Guest

    thanks a lot for the reply... However, I just mis-represented my
    problem ... Here is the updated question:


    class Aclass
    {
    double a;
    ...
    }



    class Bclass{

    Aclass B[10];
    double ...;
    ...

    }

    now in one of Bclass's member function, I want to do the following:

    fscanf(fp, "%d\n",&B.a);

    and it failed to give B.a the value from the data file.

    I know the expression "&B.a" must be wrong, but I don't know what is
    the correct form.

    Thanks,

    --Zhi







    mlimber wrote:
    > wrote:
    > > Hi,
    > >
    > > I have a question regarding reading data from a file and put it to a
    > > member of a class. I use fscanf.
    > >
    > > The class looks like this
    > >
    > > class myclass
    > > {
    > > double a;
    > > ...
    > > }
    > >
    > > Now in one of its member function, I want to do the following:
    > >
    > > fscanf(fp,"%f\n",&myclass.a);

    >
    > Inside a member function, you would do this:
    >
    > fscanf(fp,"%lf\n",&a); // Note the 'l'
    >
    > >
    > > However, it turns out that myclass.a is not given the data.
    > >
    > > I am not familiar with syntax in c++ and could anyone give me some
    > > suggestions? Thanks a lot!

    >
    > In C++, iostreams are generally preferred to C-style I/O because they
    > are type safe (e.g., you wouldn't be able omit the 'l' without doing so
    > intentionally with a nasty cast) and because you can overload the <<
    > and >> operators for your own classes. Look up how to use
    > std::ifstream. See also these FAQs:
    >
    > http://www.parashift.com/c -faq-lite/input-output.html
    >
    > Cheers! --M
    , Sep 5, 2006
    #5
  6. Guest

    "%lf" just solved my problem.

    Thanks a lot, Victor.


    Regards,
    Zhi









    Victor Bazarov wrote:
    > wrote:
    > > I have a question regarding reading data from a file and put it to a
    > > member of a class. I use fscanf.
    > >
    > > The class looks like this
    > >
    > > class myclass
    > > {
    > > double a;
    > > ...
    > > }
    > >
    > > Now in one of its member function, I want to do the following:
    > >
    > > fscanf(fp,"%f\n",&myclass.a);
    > >
    > > However, it turns out that myclass.a is not given the data.
    > >
    > > I am not familiar with syntax in c++ and could anyone give me some
    > > suggestions? Thanks a lot!

    >
    > RTFM. To convert a 'double' using 'scanf' (and its relatives), you
    > need to use '%lf' format.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask
    , Sep 5, 2006
    #6
  7. Guest

    changing "%f" to "%lf" solved the problem. &B.a is not wrong, ....

    thanks all

    wrote:
    > thanks a lot for the reply... However, I just mis-represented my
    > problem ... Here is the updated question:
    >
    >
    > class Aclass
    > {
    > double a;
    > ...
    > }
    >
    >
    >
    > class Bclass{
    >
    > Aclass B[10];
    > double ...;
    > ...
    >
    > }
    >
    > now in one of Bclass's member function, I want to do the following:
    >
    > fscanf(fp, "%d\n",&B.a);
    >
    > and it failed to give B.a the value from the data file.
    >
    > I know the expression "&B.a" must be wrong, but I don't know what is
    > the correct form.
    >
    > Thanks,
    >
    > --Zhi
    >
    >
    >
    >
    >
    >
    >
    > mlimber wrote:
    > > wrote:
    > > > Hi,
    > > >
    > > > I have a question regarding reading data from a file and put it to a
    > > > member of a class. I use fscanf.
    > > >
    > > > The class looks like this
    > > >
    > > > class myclass
    > > > {
    > > > double a;
    > > > ...
    > > > }
    > > >
    > > > Now in one of its member function, I want to do the following:
    > > >
    > > > fscanf(fp,"%f\n",&myclass.a);

    > >
    > > Inside a member function, you would do this:
    > >
    > > fscanf(fp,"%lf\n",&a); // Note the 'l'
    > >
    > > >
    > > > However, it turns out that myclass.a is not given the data.
    > > >
    > > > I am not familiar with syntax in c++ and could anyone give me some
    > > > suggestions? Thanks a lot!

    > >
    > > In C++, iostreams are generally preferred to C-style I/O because they
    > > are type safe (e.g., you wouldn't be able omit the 'l' without doing so
    > > intentionally with a nasty cast) and because you can overload the <<
    > > and >> operators for your own classes. Look up how to use
    > > std::ifstream. See also these FAQs:
    > >
    > > http://www.parashift.com/c -faq-lite/input-output.html
    > >
    > > Cheers! --M
    , Sep 5, 2006
    #7
  8. Default User Guest

    wrote:

    > thanks a lot for the reply...




    Please don't top-post. Your replies belong following or interspersed
    with properly trimmed quotes. See the majority of other posts in the
    newsgroup, or the group FAQ list:
    <http://www.parashift.com/c++-faq-lite/how-to-post.html>





    Brian
    Default User, Sep 5, 2006
    #8
    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. E11
    Replies:
    1
    Views:
    4,719
    Thomas Weidenfeller
    Oct 12, 2005
  2. Chris Torek
    Replies:
    0
    Views:
    381
    Chris Torek
    Jul 14, 2003
  3. David Rubin
    Replies:
    0
    Views:
    430
    David Rubin
    Jul 14, 2003
  4. Blankdraw

    Can't read entire record - fscanf (choked up)

    Blankdraw, Aug 25, 2003, in forum: C Programming
    Replies:
    2
    Views:
    527
    Eric Sosman
    Aug 27, 2003
  5. Jason
    Replies:
    2
    Views:
    495
    Jonathan Mcdougall
    May 13, 2006
Loading...

Share This Page