using fscanf to read data for a class member ... 
September 5th, 2006, 07:15 PM
| | | using fscanf to read data for a class member ...
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 | 
September 5th, 2006, 07:25 PM
| | | Re: using fscanf to read data for a class member ... blacksoil@gmail.com wrote: Quote:
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' Quote:
>
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 | 
September 5th, 2006, 07:35 PM
| | | Re: using fscanf to read data for a class member ... blacksoil@gmail.com wrote: Quote:
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 | 
September 5th, 2006, 07:35 PM
| | | Re: using fscanf to read data for a class member ... blacksoil@gmail.com wrote:
<snip> Quote:
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. | 
September 5th, 2006, 07:45 PM
| | | Re: using fscanf to read data for a class member ...
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: Quote: blacksoil@gmail.com wrote: Quote:
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'
> Quote:
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
| | 
September 5th, 2006, 07:45 PM
| | | Re: using fscanf to read data for a class member ...
"%lf" just solved my problem.
Thanks a lot, Victor.
Regards,
Zhi
Victor Bazarov wrote: Quote: blacksoil@gmail.com wrote: Quote:
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
| | 
September 5th, 2006, 07:45 PM
| | | Re: using fscanf to read data for a class member ...
changing "%f" to "%lf" solved the problem. &B[i].a is not wrong, ....
thanks all blacksoil@gmail.com wrote: Quote:
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: Quote: blacksoil@gmail.com wrote: Quote:
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' Quote:
>
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
| | | 
September 5th, 2006, 08:45 PM
| | | Re: using fscanf to read data for a class member ... blacksoil@gmail.com wrote: Quote: |
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 | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,840 network members.
|