关于ofstream

weicheichei 2010-11-03 05:00:18
#include<iostream>
#include<cstdlib>
#include<ctime>
#include"vector.h"
#include<fstream>
int main()
{
ofstream fout;
。。。。。。。。。。。。。。
fout.open("a.txt") fout<<"After"<<steps<<"steps,the subject""has the following location:\n";
fout<<result<<endl;
result.polar_mode();
fout<<"or\n"<<result<<endl;
fout<<"Average outward distance per step= "
<<result.magval()/steps<<endl;
steps=0;
result.set(0.0,0.0,112);
cout<<"Enter target distance(q to quit):";
}

运行以后就有错了E:\C++学习\1131\randwalk.cpp(8) : error C2065: 'ofstream' : undeclared identifier
E:\C++学习\1131\randwalk.cpp(8) : error C2146: syntax error : missing ';' before identifier 'fout'
E:\C++学习\1131\randwalk.cpp(8) : error C2065: 'fout' : undeclared identifier
E:\C++学习\1131\randwalk.cpp(31) : error C2228: left of '.open' must have class/struct/union type
E:\C++学习\1131\randwalk.cpp(33) : error C2146: syntax error : missing ';' before identifier 'fout'
E:\C++学习\1131\randwalk.cpp(33) : error C2297: '<<' : illegal, right operand has type 'char [6]'
E:\C++学习\1131\randwalk.cpp(36) : error C2297: '<<' : illegal, right operand has type 'char [4]'
E:\C++学习\1131\randwalk.cpp(38) : error C2297: '<<' : illegal, right operand has type 'char [36]'
执行 cl.exe 时出错.

1131.exe - 1 error(s), 0 warning(s)
我就是给以前可以运行的程序加了一点红色的部分,还有把后面的cout改为fout,大家给看,本是想把这个程序的运行结果输出到a.txt,中,这个文件就在工程文件夹里面放着。
...全文
361 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
weicheichei 2010-11-04
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 arm_leg 的回复:]
c/c++同时学习的后果!!
[/Quote]
你好,可不可以具体点,谢谢!
赵4老师 2010-11-04
  • 打赏
  • 举报
回复
摒弃ofstream
使用FILE *
车太靓 2010-11-04
  • 打赏
  • 举报
回复
c/c++同时学习的后果!!
龙哥依旧 2010-11-04
  • 打赏
  • 举报
回复
楼主写的啥么乱七八糟的玩意?
Linux-Torvalds 2010-11-03
  • 打赏
  • 举报
回复 1
#include<iostream>
#include<cstdlib>
#include<ctime>
#include"vector.h"
#include<fstream>
using namespace std;
int main()
{
ofstream fout;
。。。。。。。。。。。。。。
fout.open("a.txt");
fout<<"After"<<steps<<"steps,the subject""has the following location:\n";
fout<<result<<endl;
result.polar_mode();
fout<<"or\n"<<result<<endl;
fout<<"Average outward distance per step= "<<result.magval()/steps<<endl;
steps=0;
result.set(0.0,0.0,112);
cout<<"Enter target distance(q to quit):";
fout.close();
}

记得标准很重要,请写标准的C++程序。
facilbe 2010-11-03
  • 打赏
  • 举报
回复
using namespace std;
wokonglinglude 2010-11-03
  • 打赏
  • 举报
回复
还要设置文件属性
ofstream outfile;
outfile.open("textname",ofstream::out|ofstream::app);//在文件尾写入
out 打开文件作写操作 打开并清空
app 在文件尾做写操作
wokonglinglude 2010-11-03
  • 打赏
  • 举报
回复
那你编译过去了 估计 重载了<< 了
那就把 文件绑定fout.open("a.txt"); 拿到while(cin>>)循环外面 再看看
weicheichei 2010-11-03
  • 打赏
  • 举报
回复
std::ostream & operator<<(std::ostream & os, const Vector &v)
{
if(v.mode=='r')
os<<"(x,y)=("<<v.x<<","<<v.y<<")";
else if(v.mode=='p')
{
os<<"(m,a)=("<<v.mag<<","
<<v.ang*Rad_to_deg<<")";
}
else
os<<"Vector object mode is inbalid";
return os;
weicheichei 2010-11-03
  • 打赏
  • 举报
回复
这些事我自己定义的
wokonglinglude 2010-11-03
  • 打赏
  • 举报
回复
using VECTOR::Vector;
你用的标准库的vector 还是自己定义的类模板
你将一个vector 对象result 插入流中 没有定义<< 操作 要重载<<
stl的vector 输出是用迭代器的
氰客 2010-11-03
  • 打赏
  • 举报
回复
学习一下 ===
weicheichei 2010-11-03
  • 打赏
  • 举报
回复
#include<iostream>
#include<cstdlib>
#include<ctime>
#include"vector.h"
#include<fstream>
using namespace std;
int main()
{
fstream fout;
using namespace std;
using VECTOR::Vector;
srand(time(0));
double direction;
Vector step;
Vector result(0.0,0.0);
unsigned long steps=0;
double target;
double dstep;
cout<<"Enter target distance(q to quit): ";
while(cin>>target)
{
cout<<"Enter step length: ";
if(!(cin>>dstep))
break;
while (result.magval()<target)
{
direction=rand()%360;
step.set(dstep,direction,'p');
result=result+step;
steps++;
}
fout.open("a.txt");
fout<<"After"<<steps<<"steps,the subject""has the following location:\n";
fout<<result<<endl;
result.polar_mode();
fout<<"or\n"<<result<<endl;
fout<<"Average outward distance per step= "
<<result.magval()/steps<<endl;
steps=0;
result.set(0.0,0.0,112);
cout<<"Enter target distance(q to quit):";
}
cout <<"Bye!\n";
return 0;

}
这个基本上就是全部的代码了,它每次都保存着第一次的数据,怎么能让它保存所有的数据呢?

Qoo_wzp 2010-11-03
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 wokonglinglude 的回复:]
引用 8 楼 weicheichei 的回复:
引用 3 楼 wokonglinglude 的回复:
1,fout.open("a.txt") 后面加;
2'ofstream' : undeclared
是写入文件吧,那你把绑定 ofstream 对象 把要写入的东西插入对象中

我改了运行对了,就是我这个程序是循环输出的,但是在记事本里每次只保存一次输出的数据,并且是只保存第一次的,……
[/Quote]
每次用ofstream都要clear一下,用後要close文件!
wokonglinglude 2010-11-03
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 weicheichei 的回复:]
引用 3 楼 wokonglinglude 的回复:
1,fout.open("a.txt") 后面加;
2'ofstream' : undeclared
是写入文件吧,那你把绑定 ofstream 对象 把要写入的东西插入对象中

我改了运行对了,就是我这个程序是循环输出的,但是在记事本里每次只保存一次输出的数据,并且是只保存第一次的,这个又是为什么呀
[/Quote]
看不到你的 循环的代码 估计是流没clear 用的时候要保证流的状态 有一系列的close open clear 还有操作算子endl
独酌逸醉 2010-11-03
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jikeyuan1 的回复:]

LZ 你的编码太不规范了吧
不要c/c++混合使用
还有你的steps和result是什么啊 ??
[/Quote]
为什么不要C/C++混合使用呢?
weicheichei 2010-11-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wokonglinglude 的回复:]
1,fout.open("a.txt") 后面加;
2'ofstream' : undeclared
是写入文件吧,那你把绑定 ofstream 对象 把要写入的东西插入对象中
[/Quote]
我改了运行对了,就是我这个程序是循环输出的,但是在记事本里每次只保存一次输出的数据,并且是只保存第一次的,这个又是为什么呀
weicheichei 2010-11-03
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jikeyuan1 的回复:]
LZ 你的编码太不规范了吧
不要c/c++混合使用
还有你的steps和result是什么啊 ??
[/Quote]
这两个是我给变量起的名字。
jikeyuan1 2010-11-03
  • 打赏
  • 举报
回复
LZ 你的编码太不规范了吧
不要c/c++混合使用
还有你的steps和result是什么啊 ??
lams3316 2010-11-03
  • 打赏
  • 举报
回复
同意:using namespace std;
加载更多回复(4)

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧