问个文件操作的问题

ysmashimaro 2009-03-09 12:41:06
我想用c++读入一个图像文件,可是读不进来,不知是什么原因,是不是c++只能读文本文件?(我用c语言的FILE指针也不行,总是读一小段就停了,它认为到文件末尾了,但其实并没有)

我把我的代码贴出来,大家帮我看一下:


#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int main()
{
string in="E:\\mona_lisa.jpg";
string out="E:\\m.jpg";
ifstream infile;
ofstream outfile;
infile.open(in.c_str());
outfile.open(out.c_str());
if(!infile||!outfile)
{
cerr<<"error in IO!"<<endl;
return -1;
}
unsigned int point;
point=1;
while(infile>>point)
{
cout<<point<<" ";
}
cout<<hex<<point<<" ";
infile.close();
outfile.close();
return 0;
}

打印结果是1,所以连一个数也没读进去。
...全文
152 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangch_nhcmo 2009-03-09
  • 打赏
  • 举报
回复

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int main()
{
string in="a.jpg";
string out="m.jpg";
ifstream infile;
ofstream outfile;
infile.open(in.c_str());
outfile.open(out.c_str());
if(!infile||!outfile)
{
cerr<<"error in IO!"<<endl;
return -1;
}
//unsigned int point;//delete this line
//point=1; // delete this line

char point; // add this line;
while(infile>>point)
{
cout<<point<<" ";
}
cout<<hex<<point<<" ";
infile.close();
outfile.close();
return 0;
}

ysmashimaro 2009-03-09
  • 打赏
  • 举报
回复
那该怎么读啊?求教。
  • 打赏
  • 举报
回复
string in="E:\\mona_lisa.jpg";
string out="E:\\m.jpg";
你称这些为文本文件?
txt才是文本文件,图片的话貌似不能直接这样读吧?
tianma2005123 2009-03-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 qzl123666 的回复:]
三楼的加上五楼的就行了
[/Quote]

up
qzl123666 2009-03-09
  • 打赏
  • 举报
回复
三楼的加上五楼的就行了
hoomien 2009-03-09
  • 打赏
  • 举报
回复
把jpg换成BMP用LoadImage读
或者用CPicture类读。
tangshuiling 2009-03-09
  • 打赏
  • 举报
回复

试试:(最好把in,out的变量也改一下)
infile.open(in1.c_str(),ios_base::binary|ios_base::in);
outfile.open(out1.c_str(),ios_base::binary|ios_base::out);
ysmashimaro 2009-03-09
  • 打赏
  • 举报
回复
3楼的方法我也试过,好像不行遇到ff(我也不确定)就会停下来,它认为已经到了文件的末尾,所以只能读一小段,然后就停了。


#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int main()
{
string in="a.jpg";
string out="m.jpg";
ifstream infile;
ofstream outfile;
infile.open(in.c_str());
outfile.open(out.c_str());
if(!infile||!outfile)
{
cerr<<"error in IO!"<<endl;
return -1;
}
//unsigned int point;//delete this line
//point=1; // delete this line

char point; // add this line;
while(infile>>point)
{
cout<<"a";
}
cout<<hex<<point<<" ";
infile.close();
outfile.close();
return 0;
}

这样打印出来的"a"就几十个,所以根本没把文件读完就停了。

65,210

社区成员

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

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