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 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 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 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.
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
"%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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
|
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) {
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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>...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
| |