473,809 Members | 2,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with simple file I/O :)

Hello All,

I am having problem writing some data to file and I do not understand
my mistake / misconception.
I am trying to write some simple text to file but the text is rather
output to screen!

Below is my entire code from my problem, but I just want to focus on
the method "write_percenta ge_per_side_to_ file(ofstream &os)". From my
understanding I am passing an ofstream object which I declared in main(
) as "ofstream os = ("C:\\output.tx t", ios::out);" into this method,
and since I declared this as an output stream object, that using:

os << "Text"

Would write this to the filename I specified in my declaration of "os".

Any help is appreciated!

Cheers,

Kush

----------------------------------------------------------------------------------------------------------------------------------------
#include <iostream.h>
#include <math.h>
#include <fstream.h>

//*************** *************** *************** ***********Clas s
definitions
class coordinate
{
private:
int x, y;
public:
void initialize(int a, int b);
int provide_x_coord inate(void);
int provide_y_coord inate(void);
};

class side
{
private:
coordinate left_side, right_side;
public:
void initialize(int ax, int ay, int bx, int by);
double length(void);
};

class property
{
private:
side side1, side2, side3, side4;
double perimeter(void) ;
public:
void initialize(void );
void write_percentag e_per_side_to_f ile(ofstream &os);
};

//*************** *************** *************** ***********coor dinate
void coordinate::ini tialize(int a, int b)
{
x = a;
y = b;
}

int coordinate::pro vide_x_coordina te(void)
{
return x;
}

int coordinate::pro vide_y_coordina te(void)
{
return y;
}

//*************** *************** *************** ***********side
void side::initializ e(int ax, int ay, int bx, int by)
{
left_side.initi alize(ax, ay);
right_side.init ialize(bx, by);
}

double side::length(vo id)
{
int left_x, left_y, right_x, right_y;

left_x = left_side.provi de_x_coordinate ();
left_y = left_side.provi de_y_coordinate ();
right_x = right_side.prov ide_x_coordinat e();
right_y = right_side.prov ide_y_coordinat e();

return ( sqrt( ((right_x - left_x) * (right_x - left_x)) + ((right_y -
left_y) * (right_y - left_y)) ) );
}

//*************** *************** *************** ***********prop erty
void property::initi alize(void)
{
side1.initializ e(1, 6, 10, 10);
side2.initializ e(10, 10, 36, 4);
side3.initializ e(36, 4, 0, 0);
side4.initializ e(0, 0, 1, 6);
}

double property::perim eter(void)
{
return (side1.length() + side2.length() + side3.length() +
side4.length() );
}

void property::write _percentage_per _side_to_file(o fstream &os)
{
os << "The percentage length of side1 relative to the perimeter is "
<< (side1.length() / perimeter() ) * 100 << endl
<< "The percentage length of side2 relative to the perimeter is " <<
(side2.length() / perimeter() ) * 100 << endl
<< "The percentage length of side3 relative to the perimeter is " <<
(side3.length() / perimeter() ) * 100 << endl
<< "The percentage length of side4 relative to the perimeter is " <<
(side4.length() / perimeter() ) * 100 << endl;
os.close();
}

//*************** *************** *************** ***********main ()

void main(void)
{
property lot;
lot.initialize( );
ofstream os = ("C:\\output.tx t", ios::out);
lot.write_perce ntage_per_side_ to_file(os);
}
//*************** *************** *************** ***********end of program

Nov 29 '06 #1
11 1653

Kush wrote:
Hello All,

I am having problem writing some data to file and I do not understand
my mistake / misconception.
I am trying to write some simple text to file but the text is rather
output to screen!

Below is my entire code from my problem, but I just want to focus on
the method "write_percenta ge_per_side_to_ file(ofstream &os)". From my
understanding I am passing an ofstream object which I declared in main(
) as "ofstream os = ("C:\\output.tx t", ios::out);" into this method,
and since I declared this as an output stream object, that using:

os << "Text"

Would write this to the filename I specified in my declaration of "os".

Any help is appreciated!

Cheers,

Kush

----------------------------------------------------------------------------------------------------------------------------------------
#include <iostream.h>
#include <math.h>
#include <fstream.h>
#include <iostream>
#include <cmath>
#include <fstream>
>
//*************** *************** *************** ***********Clas s
definitions
class coordinate
{
private:
int x, y;
public:
void initialize(int a, int b);
int provide_x_coord inate(void);
int provide_y_coord inate(void);
};
class coordinate
{
int x, y;
public:
coordinate(int a, int b);
int getx() const;
int gety() const;
};
>
class side
{
private:
coordinate left_side, right_side;
public:
void initialize(int ax, int ay, int bx, int by);
double length(void);
};
class side
{
coordinate left_side, right_side;
public:
side(int ax, int ay, int bx, int by);
side(const coordinate& ls, const coordiante& rs);
double length() const;
}
>
class property
{
private:
side side1, side2, side3, side4;
double perimeter(void) ;
public:
void initialize(void );
void write_percentag e_per_side_to_f ile(ofstream &os);
};

//*************** *************** *************** ***********coor dinate
void coordinate::ini tialize(int a, int b)
{
x = a;
y = b;
}
coordinate::coo rdinate(int a, int b) : x(a), y(b)
{
}
>
int coordinate::pro vide_x_coordina te(void)
{
return x;
}
int coordinate::get x() const
{
return x;
}
>
int coordinate::pro vide_y_coordina te(void)
{
return y;
}

//*************** *************** *************** ***********side
void side::initializ e(int ax, int ay, int bx, int by)
{
left_side.initi alize(ax, ay);
right_side.init ialize(bx, by);
}
again, use ctor. Why? Because there are more ctors than meet the eye:
def ctor, parametized ctor, copy ctor and assignment operator - which
itself is not a ctor. If you don't write the default ctors and op=, the
compiler must generate them anyways. And the compiler will not default
initialize members unless you tell it to.
>
double side::length(vo id)
{
int left_x, left_y, right_x, right_y;

left_x = left_side.provi de_x_coordinate ();
left_y = left_side.provi de_y_coordinate ();
right_x = right_side.prov ide_x_coordinat e();
right_y = right_side.prov ide_y_coordinat e();

return ( sqrt( ((right_x - left_x) * (right_x - left_x)) + ((right_y -
left_y) * (right_y - left_y)) ) );
}

//*************** *************** *************** ***********prop erty
void property::initi alize(void)
{
side1.initializ e(1, 6, 10, 10);
side2.initializ e(10, 10, 36, 4);
side3.initializ e(36, 4, 0, 0);
side4.initializ e(0, 0, 1, 6);
}

double property::perim eter(void)
{
return (side1.length() + side2.length() + side3.length() +
side4.length() );
}

void property::write _percentage_per _side_to_file(o fstream &os)
You should be passing the std::ofstream by std::ostream reference:
void property::write _percentage_per _side_to_file(s td::ostream &os)
{
os << "The percentage length of side1 relative to the perimeter is "
<< (side1.length() / perimeter() ) * 100 << endl
<< "The percentage length of side2 relative to the perimeter is " <<
(side2.length() / perimeter() ) * 100 << endl
<< "The percentage length of side3 relative to the perimeter is " <<
(side3.length() / perimeter() ) * 100 << endl
<< "The percentage length of side4 relative to the perimeter is " <<
(side4.length() / perimeter() ) * 100 << endl;
os.close();
}

//*************** *************** *************** ***********main ()

void main(void)
{
property lot;
lot.initialize( );
ofstream os = ("C:\\output.tx t", ios::out);
Where is the error checking? How do you what, if anything, failed?

std::ofstream ofs;
ofs.open("C:\\o utput.txt");
if(!ofs || !ofs.is_open() )
{
std::cerr << "error while opening file for output.\n";
return -1;
}
lot.write_perce ntage_per_side_ to_file(os);
}
//*************** *************** *************** ***********end of program
Nov 29 '06 #2
Salt_Peter wrote:
coordinate::coo rdinate(int a, int b) : x(a), y(b)
What is this construct doing? I haven't seen a function defined like
this before.

Thanks,

jw.
Nov 29 '06 #3

James Willmott wrote:
Salt_Peter wrote:
coordinate::coo rdinate(int a, int b) : x(a), y(b)

What is this construct doing? I haven't seen a function defined like
this before.
Coders who consider pointers to be friends have few acquaintances.
Thanks,

jw.
Its constructing an instance of coordinate using an "init list" with a
and b as parameters to initialize the x and y members. Very fundamental
in C++ and usefull since the ctor does not need to allocate the 2
integers and *then* initialize their values. allocation +
initialization is simultaneously carried out.

Set a breakpoint in the ctors and watch the members x and y:

#include <iostream>

class coordinate
{
int x, y;
public:
// def ctor + init list
coordinate() : x(0), y(0)
{
std::cout << "coordinate()\n ";
}
// parametized ctor + init list
coordinate(int a, int b) : x(a), y(b)
{
std::cout << "ctor coordinate(int, int)\n";
}
// copy ctor
coordinate(cons t coordinate& copy)
{
std::cout << "copy ctor coordinate\n";
x = copy.x;
y = copy.y;
}
};

int main()
{
coordinate coord; // def ctor invoked
coordinate another(99,11); // parametized ctor invoked
coordinate coord2(coord); // copy ctor invoked
coordinate coord3 = another; // also a copy since
}

/* output:
coordinate()
ctor coordinate(int, int)
copy ctor coordinate
copy ctor coordinate
*/

Can you see why this is usefull? Its absolutely impossible to create an
instance of coordinate where x and y are left un-initialized by
mistake. Say goodbye to bugs, thanks to the creator of the class.
Next: assignment operator.

By the way, other languages also have the compiler provide default
ctors, they just don't tell you about it nor give you access to them.
C++ takes a different view, ctors are one of its cornerstones.

Nov 29 '06 #4
James Willmott wrote:
Salt_Peter wrote:
>coordinate::co ordinate(int a, int b) : x(a), y(b)
You forgot to quote the empty body:
{
}
>
What is this construct doing? I haven't seen a function defined like
this before.
It's a constructor using an initializer list. Are you sure you never saw
that? It should be explained in any book on C++.
Best

Kai-Uwe Bux
Nov 29 '06 #5
Kai-Uwe Bux wrote:
James Willmott wrote:

>>Salt_Peter wrote:

>>>coordinate:: coordinate(int a, int b) : x(a), y(b)


You forgot to quote the empty body:
{
}
>>What is this construct doing? I haven't seen a function defined like
this before.


It's a constructor using an initializer list. Are you sure you never saw
that? It should be explained in any book on C++.
My C++ book doesn't even have namespaces in it.

I've never heard of initialiser lists, but now that I know what they're
called I can research them more myself. :)

Thanks,
jw.
Nov 29 '06 #6
Kai-Uwe Bux wrote:
James Willmott wrote:

>>Salt_Peter wrote:
coordinate::c oordinate(int a, int b) : x(a), y(b)
{
}
What's the benefit of doing that versus...

coordinate::coo rdinate(int a, int b){
x=a;
y=b;
}

They seem to have the same end result at first glance?
>>What is this construct doing? I haven't seen a function defined like
this before.


It's a constructor using an initializer list. Are you sure you never saw
that? It should be explained in any book on C++.
My C++ book doesn't even have namespaces in it. :)

I've never heard of initialiser lists, but now that I know what they're
called I can research them more myself.

Thanks,
jw.
Nov 29 '06 #7
James Willmott a écrit :
Kai-Uwe Bux wrote:
James Willmott wrote:
>Salt_Peter wrote:
coordinate::co ordinate(int a, int b) : x(a), y(b)
{
}

What's the benefit of doing that versus...

coordinate::coo rdinate(int a, int b){
x=a;
y=b;
}

They seem to have the same end result at first glance?
But they don't work in the same way. The version with the initializer
list constructs x and y with the corresponding values, while the second
version construct them, then assign the value. While this is not very
important for simple types, it can make a huge difference for more
complex type where both the construction and the code for operator=()
is slower. This is also the only way to construct sub objects that
doesn't have a default constructor, or to assign references. For
instance:

class C
{
int& a;
public:
C(int val)
{
a = val;
}
};

Doesn't work, because the reference requires to be initialized before
you enter in the constructor body. You have to use a initializer list
in this case.
>What is this construct doing? I haven't seen a function defined like
this before.
Of course you did: if class B inherit class A, how does B call the
constructor of A?
My C++ book doesn't even have namespaces in it. :)
I really suggest you to pick another book then :)

Regards,

-- Emmanuel Deloget, Artware

Nov 29 '06 #8

James Willmott wrote:
Kai-Uwe Bux wrote:
James Willmott wrote:

>Salt_Peter wrote:
coordinate::co ordinate(int a, int b) : x(a), y(b)
{
}

What's the benefit of doing that versus...

coordinate::coo rdinate(int a, int b){
x=a;
y=b;
}

They seem to have the same end result at first glance?
May I suggest that you "FAQ off" .. well go and read this and the rest
of the FAQ because you'll learn a lot from it.

http://www.parashift.com/c++-faq-lit....html#faq-10.6

Nov 29 '06 #9
Emmanuel Deloget wrote:
>>What's the benefit of doing that versus...

coordinate::c oordinate(int a, int b){
x=a;
y=b;
}

They seem to have the same end result at first glance?


But they don't work in the same way. The version with the initializer
list constructs x and y with the corresponding values, while the second
version construct them, then assign the value. While this is not very
important for simple types, it can make a huge difference for more
complex type where both the construction and the code for operator=()
is slower. This is also the only way to construct sub objects that
doesn't have a default constructor, or to assign references. For
instance:

class C
{
int& a;
public:
C(int val)
{
a = val;
}
};

Doesn't work, because the reference requires to be initialized before
you enter in the constructor body. You have to use a initializer list
in this case.
class C
{
int& a;
public:
C(int val) : a(val)
{
}
};

Like this then?
>>>>What is this construct doing? I haven't seen a function defined like
this before.


Of course you did: if class B inherit class A, how does B call the
constructor of A?
Ok, yes I did, but I didn't recognise it as such.
>>My C++ book doesn't even have namespaces in it. :)


I really suggest you to pick another book then :)
I intend to.

Thanks,

jw.
Nov 29 '06 #10

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

Similar topics

2
5306
by: Cergon | last post by:
hi, i hope someone can help. Im pretty new to this php stuff and i have a slight problem. I have this script (shown below) which writes data to a text file which is then displayed on my front page. Trouble is after moving to an apache server with php on a windows platform the script no longer works. It worked fine on the unix platform. All it does now is just wipes the text file and does not write anyhing else to it, any help would be...
6
2772
by: max reason | last post by:
A method in one of my classes needs to call one of 256 other methods in the same class based on an unsigned 8-bit value (0x00 to 0xFF). How is this done? Everything I try generates errors. Something this basic can't be so difficult (or impossible) - can it? Thanks. The following roughly shows what I want: class simple { public:
1
3433
by: Proteus | last post by:
Any help appreciated on a small perl project I need to write for educator/teaching purposes. I have not programmed perl for some time, need to get up to speed, maybe some kind souls hrere will help me on this project? It looks to be a simple project, and I will start relearning pearl, but any help appreciated! I need to read and parse a simple text file (INPUT) containing multiple choice quiz questions (with titles) and answers, and...
2
1994
by: Andrew S. Giles | last post by:
OK, Ive run my head into this wall for too long. I need help. I am developing an applicaiton in C# to present a user with a GUI to specify a configurable list of machines that he wants to listen to the output of. Specify a filename to shove all of the data (into Excel), and start the whole thing going. I get that done no problem. The problem comes with the Data. The data is coming from a different application, and I am not 100% sure of...
0
5578
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
4
1699
by: Guern1 | last post by:
Hi Need a bit of help here please to point me in the right direction. I have a java class file here which i wish from a menu item to open a web page which contains a help page. ] } else if (arg.equals(" Help Page")) { } else if (arg.equals("BrowserControl")) {
1
1447
by: arun raut | last post by:
On Apr 10, 10:32 pm, Rich <R...@discussions.microsoft.comwrote: help not compilation write all easier. But Rich, Without being sure completely, if you want to use HTML as help
2
2045
by: astrogirl77 | last post by:
Hi, I'm new to Python and am hoping to find help with coding a Python script, applet. I code in an old version of Visual Basic 4.0, I have a simple app that is about 3 and a half pages of code long it does some relatively simple math additions and subtractions The problem I have is that some numbers get to be very large integers and VB automatically converts this to scientifc notation, what I need is to have all numbers added and...
1
2077
by: astrogirl77 | last post by:
I'm new to C++ and am hoping to find help with coding a simple C program, am wanting to obtain code and functioning exe's. I code in an old version of Visual Basic 4.0, I have a simple app that is about 3 and a half pages of code long it does some relatively simple math additions and subtractions The problem I have is that some numbers get to be very large integers and VB automatically converts this to scientifc notation, what I need is...
2
3337
by: vijaykumardahiya | last post by:
Hello Sir, I have a simple Issue but It is not resolve by me i.e input parameter are not store in Ms-Access. I store the input parameter through Standard Action <jsp:useBean>. jsp:useBean call a property IssueData. this property exist in SimpleBean which create a connection from DB and insert the data. At run time servlet and server also show that loggging are saved in DB. But when I open the table in Access. Its empty. Ms-Access have...
0
9602
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
10639
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
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...
0
10120
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
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6881
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
5550
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...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.