Connecting Tech Pros Worldwide Forums | Help | Site Map

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

blacksoil@gmail.com
Guest
 
Posts: n/a
#1: Sep 5 '06
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


mlimber
Guest
 
Posts: n/a
#2: Sep 5 '06

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

Victor Bazarov
Guest
 
Posts: n/a
#3: Sep 5 '06

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


Bart
Guest
 
Posts: n/a
#4: Sep 5 '06

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.

blacksoil@gmail.com
Guest
 
Posts: n/a
#5: Sep 5 '06

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
blacksoil@gmail.com
Guest
 
Posts: n/a
#6: Sep 5 '06

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
blacksoil@gmail.com
Guest
 
Posts: n/a
#7: Sep 5 '06

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
Default User
Guest
 
Posts: n/a
#8: Sep 5 '06

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
Closed Thread