帮助简单的文件I / O :) [英] Help with simple file I/O :)
问题描述
大家好,
我在将一些数据写入文件时遇到问题,我不明白
我的错误/误解。
我正在尝试写一些简单的文本到文件,但文本相当
输出到屏幕!
以下是我的问题的全部代码,但我只想关注
方法write_percentage_per_side_to_file(ofstream& os)。从我的
理解我传递的一个ofstream对象,我在main(
)中声明为ofstream os =(" C:\\\\ output.txt ;,ios :: out);进入这个方法,
,因为我将其声明为输出流对象,使用:
os<< 文本
将此写入我在os声明中指定的文件名。
任何帮助表示赞赏!
干杯,
Kush
----------- -------------------------------------------------- -------------------------------------------------- -------------------------
#include< iostream.h>
#包括< math.h>
#include< fstream.h>
// ************* ************************************* ******班级
定义
班级坐标
{
私人:
int x,y;
public:
void initialize(int a,int b);
int provide_x_coordinate(void);
int provide_y_coordinate(void) ;
};
班级
{
私人:
坐标left_side,right_side;
public:
void initialize(int ax,int ay,int b x,int by);
双倍长度(无效);
};
类属性
{
私人:
side side1,side2,side3,side4;
双倍周长(无效);
public:
void initialize(void);
void write_percentage_per_side_to_file(ofstream& os);
};
// ***************************************** ********* ******坐标
void coordinate :: initialize(int a,int b)
{
x = a;
y = b;
}
int coordinate :: provide_x_coordinate(void)
{
返回x;
}
int coordinate :: provide_y_coordinate(void)
{
返回y;
}
// ************ ************************************** ******方
void side :: initialize(int ax,int ay,int bx,int by)
{
left_side.initialize(ax,ay);
right_side.initialize(bx,by );
}
双方::长度(无效)
{
int left_x ,left_y,right_x,right_y;
left_x = left_side.provide_x_coordinate();
left_y = left_side.provide_y_coordinate();
right_x = right_side.provide_x_coordinate();
right_y = right_side.provide_y_coordinate();
return(sqrt(((right_x - left_x)*(right_x - left_x))+((right_y -
left_y)*(right_y - left_y))));
}
// ********* ** ******属性
void property :: initialize(void)
{
side1.initialize(1,6, 10,10);
side2.initialize(10,10,36,4);
side3.initialize(36,4,0,0);
side4.initialize(0,0,1,6);
}
double property :: perimeter(void)
{
return(side1.length()+ side2.length()+ side3.length()+
side4.length());
}
void property :: write_percentage_per_side_to_file(ofstre am& os)
{
os<< side1相对于周长的百分比长度是
<< (side1.length()/ perimeter())* 100<< endl
<< 边2相对于周长的百分比长度是 <<
(side2.length()/ perimeter())* 100<< endl
<< side3相对于周长的百分比长度是 <<
(side3.length()/ perimeter())* 100<< endl
<< side4相对于周长的百分比长度是 <<
(side4.length()/ perimeter())* 100<< endl;
os.close();
}
// *********** *************************************** ****** main()
void main(无效)
{
物业地段;
lot.initialize();
ofstream os =(" C:\\output.txt",ios :: out);
lot.write_percentage_per_side_to_file(os);
}
// ************************************ ************** ******程序结束
Kush写道:
Hello All,
我在将一些数据写入文件时遇到问题我不明白
我的错误/误解。
我正在尝试写一些简单的文本到文件,但文字相当
输出到屏幕!
以下是我的问题的全部代码,但我只想关注
方法write_percentage_per_side_to_file(ofstream& amp) ; OS)" ;.从我的
理解我传递的一个ofstream对象,我在main(
)中声明为ofstream os =(" C:\\\\ output.txt ;,ios :: out);进入这个方法,
,因为我将其声明为输出流对象,使用:
os<< 文本
将此写入我在os声明中指定的文件名。
任何帮助表示赞赏!
干杯,
Kush
----------- -------------------------------------------------- -------------------------------------------------- -------------------------
#include< iostream.h>
#包括< math.h>
#include< fstream.h>
#include< iostream>
#include< cmath>
#include< fstream>
>
// ********************** **************************** ******班级
定义
class coordinate
{
private:
int x,y;
public:
void initialize(int a,int b);
int provide_x_coordinate(void);
int provide_y_coordinate(void);
};
班级坐标
{
int x,y;
public:
坐标(int a,int b);
int getx()const;
int gety()const;
};
>
班级
{
私人:
坐标left_side,right_side;
public:
void initialize(int ax,int ay,int bx,int by);
双倍长度(无效);
};
班级一侧
{
坐标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_percentage_per_side_to_file(ofstream& os);
};
// **************************************** ********** ******坐标
void coordinate :: initialize(int a,int b)
{
x = a;
y = b;
}
coordinate :: coordinate(int a ,int b):x(a),y(b)
{
}
>
int coordinate :: provide_x_coordinate(void)
{
返回x;
}
int coordinate :: getx()const
{
返回x;
}
>
int coordinate :: provide_y_coordinate(void)
{
返回y;
}
// ********************************** **************** ******方
void side :: initialize(int ax,int ay,int bx,int by)
{
left_side.initialize(ax,ay);
right_side.initialize(bx,by);
}
再次使用ctor。为什么?因为有更多的因素而不是眼睛:
def ctor,参数化的ctor,复制ctor和赋值运算符 -
本身不是ctor。如果你不编写默认的ctors和op =,那么
编译器必须生成它们。并且编译器不会默认
初始化成员,除非你告诉它。
>
双方:: length(无效)
{
int left_x,left_y,right_x,right_y;
left_x = left_side.provide_x_coordinate( );
left_y = left_side.provide_y_coordinate();
right_x = right_side.provide_x_coordinate();
right_y = right_side.provide_y_coordinate();
返回(sqrt(((right_x - left_x)*(right_x - left_x))+((right_y -
left_y)*(right_y - left_y) )));
}
// *********************** *************************** ******物业
void property :: initialize(void)
{
side1.initialize(1,6,10,10);
side2.initialize(10,10,36,4) ;
side3.initialize(36,4,0,0);
side4.initialize(0,0,1,6);
}
double property :: perimeter(void)
{
return(side1.length()+ side2.length()+ side3.length()+
side4.length());
}
void property :: write_percentage_per_side_to_file(ofstre am& os)
You应该通过std :: ostream引用传递std :: ofstream:
void property :: write_percentage_per_side_to_file(std :: o stream& os)
{
os<< side1相对于周长的百分比长度是
<< (side1.length()/ perimeter())* 100<< endl
<< 边2相对于周长的百分比长度是 <<
(side2.length()/ perimeter())* 100<< endl
<< side3相对于周长的百分比长度是 <<
(side3.length()/ perimeter())* 100<< endl
<< side4相对于周长的百分比长度是 <<
(side4.length()/ perimeter())* 100<< endl;
os.close();
}
// *********** *************************************** ****** main()
void main(无效)
{
物业地段;
lot.initialize();
ofstream os =(" C:\\output.txt",ios :: out);
错误检查在哪里?你怎么样,如果有的话,失败了?
std :: ofstream ofs;
ofs.open(" C:\\ output。 txt");
if(!ofs ||!ofs.is_open())
{
std :: cerr<< 打开输出文件时出错。\ nn;
返回-1;
}
lot.write_percentage_per_side_to_file(os);
}
// *********************** *************************** ******程序结束
Salt_Peter写道:
坐标:: coordinate(int a,int b):x(a),y(b)
这个构造在做什么?我以前没见过像
这样定义的功能。
谢谢,
jw。
James Willmott写道:
Salt_Peter写道:
< blockquote class =post_quotes>
坐标:: coordinate(int a,int b):x(a),y(b)
什么是这个构造做什么?我之前没有看到像
这样定义的函数。
考虑指针成为朋友的编码员很少有熟人。
谢谢,
jw。
它使用初始化列表构建坐标实例。使用
和b作为初始化x和y成员的参数。非常基础
在C ++中很有用,因为ctor不需要分配2
整数和*然后*初始化它们的值。分配+
同时进行初始化。
在ctors中设置断点并观察成员x和y:
#include< iostream>
班级坐标
{
int x,y;
public:
// def ctor + init list
coordinate():x(0),y(0)
{
std :: cout<< " coordinate()\ n";
}
//参数化ctor + init列表
坐标(int a,int b) :x(a),y(b)
{
std :: cout<< " ctor coordinate(int,int)\ n";
}
//复制ctor
坐标(const坐标和复制)
{
std :: cout<< copy ctor coordinate\\\
;
x = copy.x;
y = copy.y;
}
};
int main()
{
坐标坐标; //调用def ftor
坐另一个(99,11); //调用参数化的ctor
坐标coord2(coord); //复制ctor调用
坐标coord3 =另一个; //也是一份自
}
/ *输出:
坐标()
ctor coordinate(int,int)
copy ctor coordinate
copy ctor coordinate
* /
$ b $你能明白为什么这很有用吗?绝对不可能创建一个
坐标实例,其中x和y未被z / $
错误初始化。告别错误,多亏了这个类的创建者。
下一个:赋值运算符。
顺便说一句,其他语言也有编译器提供的默认值
ctors,他们只是没有告诉你它,也没有让你访问它们。
C ++采取不同的观点,ctors是它的基石之一。 />
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_percentage_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.txt", 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>
//************************************************** ******Class
definitions
class coordinate
{
private:
int x, y;
public:
void initialize(int a, int b);
int provide_x_coordinate(void);
int provide_y_coordinate(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_percentage_per_side_to_file(ofstream &os);
};
//************************************************** ******coordinate
void coordinate::initialize(int a, int b)
{
x = a;
y = b;
}
int coordinate::provide_x_coordinate(void)
{
return x;
}
int coordinate::provide_y_coordinate(void)
{
return y;
}
//************************************************** ******side
void side::initialize(int ax, int ay, int bx, int by)
{
left_side.initialize(ax, ay);
right_side.initialize(bx, by);
}
double side::length(void)
{
int left_x, left_y, right_x, right_y;
left_x = left_side.provide_x_coordinate();
left_y = left_side.provide_y_coordinate();
right_x = right_side.provide_x_coordinate();
right_y = right_side.provide_y_coordinate();
return ( sqrt( ((right_x - left_x) * (right_x - left_x)) + ((right_y -
left_y) * (right_y - left_y)) ) );
}
//************************************************** ******property
void property::initialize(void)
{
side1.initialize(1, 6, 10, 10);
side2.initialize(10, 10, 36, 4);
side3.initialize(36, 4, 0, 0);
side4.initialize(0, 0, 1, 6);
}
double property::perimeter(void)
{
return (side1.length() + side2.length() + side3.length() +
side4.length() );
}
void property::write_percentage_per_side_to_file(ofstre am &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.txt", ios::out);
lot.write_percentage_per_side_to_file(os);
}
//************************************************** ******end of program
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_percentage_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.txt", 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>
>
//************************************************** ******Class
definitions
class coordinate
{
private:
int x, y;
public:
void initialize(int a, int b);
int provide_x_coordinate(void);
int provide_y_coordinate(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_percentage_per_side_to_file(ofstream &os);
};
//************************************************** ******coordinate
void coordinate::initialize(int a, int b)
{
x = a;
y = b;
}coordinate::coordinate(int a, int b) : x(a), y(b)
{
}
>
int coordinate::provide_x_coordinate(void)
{
return x;
}int coordinate::getx() const
{
return x;
}
>
int coordinate::provide_y_coordinate(void)
{
return y;
}
//************************************************** ******side
void side::initialize(int ax, int ay, int bx, int by)
{
left_side.initialize(ax, ay);
right_side.initialize(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(void)
{
int left_x, left_y, right_x, right_y;
left_x = left_side.provide_x_coordinate();
left_y = left_side.provide_y_coordinate();
right_x = right_side.provide_x_coordinate();
right_y = right_side.provide_y_coordinate();
return ( sqrt( ((right_x - left_x) * (right_x - left_x)) + ((right_y -
left_y) * (right_y - left_y)) ) );
}
//************************************************** ******property
void property::initialize(void)
{
side1.initialize(1, 6, 10, 10);
side2.initialize(10, 10, 36, 4);
side3.initialize(36, 4, 0, 0);
side4.initialize(0, 0, 1, 6);
}
double property::perimeter(void)
{
return (side1.length() + side2.length() + side3.length() +
side4.length() );
}
void property::write_percentage_per_side_to_file(ofstre am &os)You should be passing the std::ofstream by std::ostream reference:
void property::write_percentage_per_side_to_file(std::o stream &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.txt", ios::out);Where is the error checking? How do you what, if anything, failed?
std::ofstream ofs;
ofs.open("C:\\output.txt");
if(!ofs || !ofs.is_open() )
{
std::cerr << "error while opening file for output.\n";
return -1;
}
lot.write_percentage_per_side_to_file(os);
}
//************************************************** ******end of program
Salt_Peter wrote:
coordinate::coordinate(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.
James Willmott wrote:Salt_Peter wrote:
coordinate::coordinate(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(const 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.
这篇关于帮助简单的文件I / O :)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!