Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > using fscanf to read data for a class member ...

Reply
Thread Tools

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

 
 
blacksoil@gmail.com
Guest
Posts: n/a
 
      09-05-2006
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

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      09-05-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-05-2006
(E-Mail Removed) 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


 
Reply With Quote
 
Bart
Guest
Posts: n/a
 
      09-05-2006
(E-Mail Removed) 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.

 
Reply With Quote
 
blacksoil@gmail.com
Guest
Posts: n/a
 
      09-05-2006
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[i].a);

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

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

Thanks,

--Zhi







mlimber wrote:
> (E-Mail Removed) 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


 
Reply With Quote
 
blacksoil@gmail.com
Guest
Posts: n/a
 
      09-05-2006
"%lf" just solved my problem.

Thanks a lot, Victor.


Regards,
Zhi









Victor Bazarov wrote:
> (E-Mail Removed) 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


 
Reply With Quote
 
blacksoil@gmail.com
Guest
Posts: n/a
 
      09-05-2006
changing "%f" to "%lf" solved the problem. &B[i].a is not wrong, ....

thanks all

(E-Mail Removed) 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[i].a);
>
> and it failed to give B.a the value from the data file.
>
> I know the expression "&B[i].a" must be wrong, but I don't know what is
> the correct form.
>
> Thanks,
>
> --Zhi
>
>
>
>
>
>
>
> mlimber wrote:
> > (E-Mail Removed) 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


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      09-05-2006
(E-Mail Removed) 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
 
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
What is the correct grammar to make a function call by using static member data which is a pointer to a ordinary class member function? zaeminkr@gmail.com C++ 3 07-06-2007 12:50 PM
Class Member Data and Member Function Parameters - Should Parameters Be Data Members? Jason C++ 2 05-13-2006 07:11 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Re: How to put comments in initialization file read by fscanf()? David Rubin C Programming 0 07-14-2003 09:14 PM
Re: How to put comments in initialization file read by fscanf()? Chris Torek C Programming 0 07-14-2003 05:33 AM



Advertisments