473,320 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

Sep 5 '06 #1
7 3106

bl*******@gmail.com 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

Sep 5 '06 #2
bl*******@gmail.com 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
Sep 5 '06 #3
bl*******@gmail.com 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.

Sep 5 '06 #4
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:
bl*******@gmail.com 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
Sep 5 '06 #5
"%lf" just solved my problem.

Thanks a lot, Victor.
Regards,
Zhi



Victor Bazarov wrote:
bl*******@gmail.com 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
Sep 5 '06 #6
changing "%f" to "%lf" solved the problem. &B[i].a is not wrong, ....

thanks all

bl*******@gmail.com 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:
bl*******@gmail.com 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
Sep 5 '06 #7
bl*******@gmail.com 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
Sep 5 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Thomas Sourmail | last post by:
Hi, I hope I am missing something simple, but.. here is my problem: I need my program to check the last column of a file, as in : a b c d target ref 0 0 0 0 1 a 1 0 0 0 1.5 b 2 0 0 0 2 c
1
by: siliconwafer | last post by:
Hi All, here is one code: int main() { FILE*fp; unsigned long a; fp = fopen("my_file.txt","w+"); a = 24; fprintf(fp,"%ld",a); while(fscanf(fp,"%ld",&a) == 1) {
7
by: fakeprogress | last post by:
For a homework assignment in my Data Structures/C++ class, I have to create the interface and implementation for a class called Book, create objects within the class, and process transactions that...
4
by: John | last post by:
I need to read data from the file like the following with name and score, but some line may only has name without score: joe 100 amy 80 may Here's my code, but it couldn't read the line with...
37
by: PeterOut | last post by:
I am using MS Visual C++ 6.0 on Windows XP 5.1 (SP2). I am not sure if this is a C, C++ or MS issue but fscanf has been randomly hanging on me. I make the call hundreds, if not thousands, of...
10
by: rsk | last post by:
Hi Friends, I have written a code which suppose to read all the numbers from a hex file,But to my surprise the code is skiping every alternate value.Don't know why? Can you please help me in...
3
by: Ranioo | last post by:
Hello everybody: i'm a new member in this site and i realy have a problem with c++. ok my problem is : i want to read text from a file and the text contains characters and decimal and...
1
by: Matrixinline | last post by:
Hi All, File Text.txt Contains following text as : "C:\program file\application data\details\app" "D:\Program File" I tried to read that data as fscanf(oFp, "%s %s", sCopyDirectory,...
24
by: kindrain | last post by:
the code checks whether a.txt has exact the same lines, then write different lines into b.txt Here it is: (before that ,you should creat a.txt) ---------------------- #include<stdio.h>...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.