Connecting Tech Pros Worldwide Help | Site Map

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

  #1  
Old September 5th, 2006, 08:15 PM
blacksoil@gmail.com
Guest
 
Posts: n/a
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

  #2  
Old September 5th, 2006, 08:25 PM
mlimber
Guest
 
Posts: n/a

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

  #3  
Old September 5th, 2006, 08:35 PM
Victor Bazarov
Guest
 
Posts: n/a

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


  #4  
Old September 5th, 2006, 08:35 PM
Bart
Guest
 
Posts: n/a

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.

  #5  
Old September 5th, 2006, 08:45 PM
blacksoil@gmail.com
Guest
 
Posts: n/a

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
  #6  
Old September 5th, 2006, 08:45 PM
blacksoil@gmail.com
Guest
 
Posts: n/a

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
  #7  
Old September 5th, 2006, 08:45 PM
blacksoil@gmail.com
Guest
 
Posts: n/a

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
  #8  
Old September 5th, 2006, 09:45 PM
Default User
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 04:15 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 11:37 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 09:56 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 03:15 AM