二进制文件如何判断结束?

wvins 2006-01-18 08:47:45
int main(int argc,char ** argv)
{
ifstream in("某个JPG文件");
ofstream out("另一个JPG文件");
char c;
while(!in.eof()) //或者是while(in>>c)也一样
{in>>c;
out<<c;
}
in.close();
out.close();
cin.get();
return 0;
}
文件总是拷贝到一部分就不进行了
--------使用windiff显示前面部分一致!
请教那里有问题
...全文
923 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
wvins 2006-01-19
  • 打赏
  • 举报
回复
大家都说了我把我从从邮件列表中学到的也贴出来吧

stream will be close while got 0x00 if open mode is not binary;

for ofstream: ios::out | ios::binary
for ifstream: ios::in | ios::binary

then use in.get(c) or out.put(c);

直接读入buffer速度将大大提高: in.rdbuf();

Seben 2006-01-19
  • 打赏
  • 举报
回复
I believe you can add an output stream into the code above easily. Please look up fread in MSDN for its params and return value.
Seben 2006-01-19
  • 打赏
  • 举报
回复
My solution is using the fread function. Please see below:

FILE * input = fopen("file path", "r");
if (input == NULL)
return false;

int result = _setmode(_fileno(input), _O_BINARY ); // set file mode as Binary
if (result == -1)
{
perror( "Cannot set mode" );
fclose(input);
return false;
}

fseek(input, 0L, SEEK_SET);

const int count = BlockCount; // suppose BlockCount = 100 or 1024, etc.
unsigned char buf[count + 1] = {0};

int validCount; // the byte number of the valid data getting from the file
do
{
validCount = fread(buf, sizeof(char), count, input);

// output buf here

}
while (validCount == count);

fclose(output);

Kid4you 2006-01-19
  • 打赏
  • 举报
回复
不要用char c这个东东来读,用个string比较好.
zylthinking 2006-01-19
  • 打赏
  • 举报
回复
#include <fstream>

int main(int argc,char ** argv)
{
ifstream in("某个JPG文件");
ofstream out("另一个JPG文件");
out << in.rdbuf();
return 0;
}
Bible_Chou 2006-01-19
  • 打赏
  • 举报
回复
//但是奇怪的是下面的循环条件只能使用in.get(c)
//使用in>>c或者!in.eof()都不行
while(in.get(c)) //或者是while(in>>c)也一样
{
out<<c;
count++;
}
Bible_Chou 2006-01-19
  • 打赏
  • 举报
回复
//以下两种方法都可以
//C
#include <stdio.h>
#include <process.h>
#include <conio.h>
int main(void)
{
FILE *fpr=NULL;
FILE *fpw=NULL;
int count=0;
fpr=fopen("test.jpg","rb");
fpw=fopen("result.jpg","wb");
if(fpr==NULL||fpw==NULL)
{
printf("Error!\n");
getche();
exit(0);
}
char temp=0;
count=0;
while(!feof(fpr))
{
fread(&temp,1,1,fpr);
if( ferror( fpr ) )
{
perror( "Read error" );
break;
}
count++;
//putchar(temp);
fwrite(&temp,1,1,fpw);
}
printf("\n=======================================================================\n");
printf("\t\tSucced!\n");
printf("\n=======================================================================\n");
fclose(fpw);
fclose(fpr);
//getche();


}



//Cpp
#include <iostream>
#include <fstream>
int main(int argc,char ** argv)
{
using namespace std;
int count =0;
ifstream in;
ofstream out;
in.open("test.jpg",ios::in|ios::binary);
out.open("result.jpg",ios::out|ios::binary);
char c;
count = 0;
while(in.get(c)) //或者是while(in>>c)也一样
{
out<<c;
count++;
}
in.close();
out.close();
//cin.get();
return 0;
}


wvins 2006-01-19
  • 打赏
  • 举报
回复
out << in.rdbuf();
不会用,似乎陷入死循环!
现在玩的是一台游戏机,没一点开发工具,更别说文档了。

临时装个DEV-CPP测了一下!
wvins 2006-01-19
  • 打赏
  • 举报
回复
in.get(c)
方法可行
zylthinking 2006-01-19
  • 打赏
  • 举报
回复
我试验了一下,测试函数包括get(),get(char&), rdbuf(), >>, 只有>> 有这鸟问题,应该是 operator >> () 实现缺陷造成的吧.

不过我的是linux下测试的,windows下不知道如何.
具体测试请看 http://blog.csdn.net/zylthinking/archive/2006/01/19/583516.aspx.

逸学堂 2006-01-18
  • 打赏
  • 举报
回复
while(!in.eof()) //或者是while(in>>c)也一样
{in>>c;
out<<c;
}

楼主也没有以二进制方式打开啊,如果是文本方式打开
string str;
while(in.getline(str,'\n'))
{
}

如果是二进制方式打开
基于CFile类的.用STL方式,
自己在看看帮助!
要加上ios_base::binary

int itemp;
while(itemp = in.read(str,20))
{
out.write(str,itemp)
}
cunsh 2006-01-18
  • 打赏
  • 举报
回复
out << in.rdbuf();
zylthinking 2006-01-18
  • 打赏
  • 举报
回复
EOF 在目前平台上大都是 0XFFFFFFFF,那么假如JPG文件中间冒出这四个字节, 用in.eof()当然返回true

建议用 readsome试一试
wvins 2006-01-18
  • 打赏
  • 举报
回复
当然,我编译执行的是含有binary的,
结果是半截的.
如果使用默认文本方式的,大家也应该知道很快会终止!
wvins 2006-01-18
  • 打赏
  • 举报
回复
sorry
漏掉了,
ifstream in("文件名",ios::binary)
cunsh 2006-01-18
  • 打赏
  • 举报
回复
还有楼主的打开文件的方式是文本.
楼主打开是图片文件吗?
cunsh 2006-01-18
  • 打赏
  • 举报
回复
while(!in.eof())
{in>>c;
out<<c;
}

假如in文件的内容为 1 2 3 EOF
当读完3时. 输入流in没有读出EOF. 进入下一轮循环了.
判断 !in.eof() 为真. 所以 in >> c ; out << c; 了.

也就是只有在EOF被读出的时候 in 的eof标志才是1啊.
while(1)
{
in>>c;
if(in.eof())break;
out<<c;
}
wvins 2006-01-18
  • 打赏
  • 举报
回复
会不会和CHAR C有关?
sankt 2006-01-18
  • 打赏
  • 举报
回复
问题比较奇怪
while(!in.eof()) //这样应该可以的

关注。。。


64,281

社区成员

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

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