关于fstream的一个问题

wanghao2979 2007-04-30 12:24:20
是这样的
我在学习fstream的时候发现了一个问题
我用ofstream想把文件调入到内存中
我先定义了个数组
这是我的码
大家帮我指出错误
#include <iostream>
#include <string>
#include <tchar.h>
#include <fstream>
using namespace std;
int main()
{
TCHAR =L a[10000];
ofstream f("c:\\dos.txt");
f>>a[10000];
system("pause");
return 0;
}
...全文
250 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
FingerStyle 2007-04-30
  • 打赏
  • 举报
回复
搂住好像刚学c++, 那就不急, 以后自然回知道的。

百度一下
STL
wanghao2979 2007-04-30
  • 打赏
  • 举报
回复
vector<TCHAR> tcV;
ifstream f("c:\\a.txt");
copy(istream_iterator<TCHAR>(f), istream_iterator<TCHAR>(), back_inserter(tcV));

这段码能加个注释简单解释一下吗?
问题1.vector是什么,在这什么用?
2.copy的用法我自己去看看去
学习了
谢谢在学习的道路上帮助过我的人
我会记住你们的
FingerStyle 2007-04-30
  • 打赏
  • 举报
回复
vector<TCHAR> tcV;
ifstream f("c:\\a.txt");
copy(istream_iterator<TCHAR>(f), istream_iterator<TCHAR>(), back_inserter(tcV));
wanghao2979 2007-04-30
  • 打赏
  • 举报
回复
没看过标准跌代器
不知道是啥
我去baidu自己看去
wanghao2979 2007-04-30
  • 打赏
  • 举报
回复
那么如果不知道a[]多大
我就想定义个越大越好的数组
jixingzhong 2007-04-30
  • 打赏
  • 举报
回复
f>>a[]和copy(istream_iterator<TCHAR>f,istream_iterator<TCHAR>(),a)
有什么区别?
=======
f>>a[] //没有这种用法

把ifstream 换成 cin,
你见过 cin>>a[10000] 这样输入 10000 个数据的么?

如果要 f>>a[] 这样的方式,
那么使用 for 逐个输入:
for (int i=0; i<1000; i++)
{
f>>a[i] ;
}
jixingzhong 2007-04-30
  • 打赏
  • 举报
回复
istream_iterator是什么意思 ?

是C++中的一种迭代器,
看看C++标准迭代器吧。
wanghao2979 2007-04-30
  • 打赏
  • 举报
回复
f>>a[]和copy(istream_iterator<TCHAR>f,istream_iterator<TCHAR>(),a)
有什么区别?
请一个一个回答我的问题
谢谢
FingerStyle 2007-04-30
  • 打赏
  • 举报
回复
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>
#include <tchar.h>
using namespace std;
int main()
{
TCHAR a[1000];
ifstream f("c:\\a.txt");
copy(istream_iterator<TCHAR>(f), istream_iterator<TCHAR>(), a);
for (int i=0; i<1000; i++)
{
cout << a[i] ;
}
return 0;
}
jixingzhong 2007-04-30
  • 打赏
  • 举报
回复
f>>a[9999];


=========
用 copy 算法:
copy(istream_iterator<TCHAR>(f), istream_iterator<TCHAR>(), a);
FingerStyle 2007-04-30
  • 打赏
  • 举报
回复
for (int i=0; i<9999; i++)
{
f>>a[i];
}
wanghao2979 2007-04-30
  • 打赏
  • 举报
回复
楼上的
istream_iterator是什么意思 ?
wanghao2979 2007-04-30
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <tchar.h>
#include <fstream>
using namespace std;
int main()
{
char a[10000];
int i;
ifstream f("c:\\dos.txt");
f>>a[9999];
for(i=0;i<9999;i++)cout<<a[i];
system("pause");
return 0;
}
这样还是错的
错误的地方在 输出的都是错误的字
FingerStyle 2007-04-30
  • 打赏
  • 举报
回复
TCHAR a[1000];
ifstream f("c:\\dos.txt");
copy(istream_iterator<TCHAR>(f), istream_iterator<TCHAR>(), a);
wanghao2979 2007-04-30
  • 打赏
  • 举报
回复

应该用ifstream
FingerStyle 2007-04-30
  • 打赏
  • 举报
回复
ofstream f("c:\\dos.txt");
f>>a[10000];
????????????
in ??
wanghao2979 2007-04-30
  • 打赏
  • 举报
回复
d:\cc\wanghao\wanghao\cc.cpp(13): 参见对正在编译的类 模板 实例化“std::istream_iterator<_Ty>”的引用
with
[
_Ty=TCHAR
]
istream_iterator<TCHAR>(f)
他是非法的

33,317

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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