C++中文件问题

Ansyral 2009-04-27 08:52:16
我数据结构实验是哈弗曼编码系统,要求将结果写入文件,由于文件要在函数中使用,我把它们定义为全局流变量,可是文件却无法生成。我又写了个小程序验证一下,如下,但也没生成文件,想问问大家为什么呢?谢谢各位了~~
#include<iostream>
#include<fstream>
using namespace std;
fstream a;
int main()
{
a.open("2.txt",ios::in|ios::out);
if(!a)cout<<"wrong";
return 0;
}
不胜感激……
...全文
110 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
binghe2007 2009-04-30
  • 打赏
  • 举报
回复
用封装好的TFileStream 类操作。
Ansyral 2009-04-29
  • 打赏
  • 举报
回复
可是C++中定义了既可读也可写的方式,为什么我那么用会生成不了文件呢?[Quote=引用 4 楼 liao05050075 的回复:]
你就在要写出的时候用a.open("2.txt",ios::out);

在要读入的时候,关闭已打开的文件,然后用再a.open("2.txt",ios::in)打开它
[/Quote]
Ansyral 2009-04-29
  • 打赏
  • 举报
回复
top()函数的返回值是栈顶元素,你没赋值,也没写入文件,当然文件中什么也没有了……
[Quote=引用 6 楼 cpp_yangzuoshi 的回复:]
不显示到屏幕上直接打开txt文件可不可以看到读入的数据?有的可以,有的不可以?!
我用栈写了一个小程序,打开txt文件后看不到输入的内容,但是不用栈却能看到,不知为何?
#include <stack>
#include "fstream"
#include <iostream>

using namespace std;

int main ()
{
ofstream output;
input.open("stackNumber.txt");
stack <int> numbers;
int item;
cin >> item;
numbers.push(item);
n…
[/Quote]
leo315 2009-04-28
  • 打赏
  • 举报
回复
mark
Yanger_xy 2009-04-27
  • 打赏
  • 举报
回复
不显示到屏幕上直接打开txt文件可不可以看到读入的数据?有的可以,有的不可以?!
我用栈写了一个小程序,打开txt文件后看不到输入的内容,但是不用栈却能看到,不知为何?
#include <stack>
#include "fstream"
#include <iostream>

using namespace std;

int main ()
{
ofstream output;
input.open("stackNumber.txt");
stack<int> numbers;
int item;
cin >> item;
numbers.push(item);
numbers.top();
numbers.pop();
out.close();
return 0;
}
dinjay 2009-04-27
  • 打赏
  • 举报
回复
LS正解
liao05050075 2009-04-27
  • 打赏
  • 举报
回复
你就在要写出的时候用a.open("2.txt",ios::out);

在要读入的时候,关闭已打开的文件,然后用再a.open("2.txt",ios::in)打开它


Ansyral 2009-04-27
  • 打赏
  • 举报
回复
我这个文件既要写也要读,因为系统中要把编码、译码写入文件(写);还提供一个打印的功能,就是屏显文件(读);
lingyin55 2009-04-27
  • 打赏
  • 举报
回复

#include <iostream>
#include <fstream>
using namespace std;
fstream a;
int main()
{
a.open("2.txt",ios::out); /////////
if(!a)cout <<"wrong";
return 0;
}
jackyjkchen 2009-04-27
  • 打赏
  • 举报
回复
a.open("2.txt",ios::out); 把in去了,既然要输出,out就行了

65,210

社区成员

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

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