473,779 Members | 2,078 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3143

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.c om/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
5458
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
2215
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
2958
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 manipulate (and report on) members of the class. Interface consists of: - 5 private variables char author; char title; char code;
4
4234
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 "may" because there is no score. Anyone knows what is the workaround to this problem?
37
4981
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 times but it hangs in different places with the same data. The offending code follows. ReadFile(char *csFileName) { float fFloat1, fFloat2;
10
3671
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 solving this problem. The code is as follows;
3
1839
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 hexadecimal using fscanf but when i debug (run the program) i only get smiley faces...??? but when i used getc() it worked ok!!! but the homework is with using fscanf()?????? this my program and i hope someone responds very fast i'm running out of...
1
9363
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, sToDirectory);
24
6020
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> #include<string.h> void main(){ FILE *fp,*ftemp;
0
9636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10139
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9931
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6727
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.