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

B

blacksoil

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
 
M

mlimber

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
 
V

Victor Bazarov

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
 
B

Bart

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.
 
B

blacksoil

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
 
B

blacksoil

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

thanks all

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






Inside a member function, you would do this:

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


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top