首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于文件的读写操作

2012-02-20 
关于文件的读写操作,请教大家一批文件大小都是1M多用16进制打开开头部分都是00000000只有结尾部分的数据是

关于文件的读写操作,请教大家
一批文件   大小都是1M多   用16进制打开   开头部分都是00   00   00   00   只有结尾部分的数据是有效的数据。
请问如何编写一个程序,把前面   的00   00   00   的无效数据去掉。

小弟刚开始学,请教各位高手,如何才能实现。最好给个类似的例子。谢谢了。


[解决办法]
#include <fstream>
#include <iterator>
#include <deque>

ifstream ifs( "xxx ", ios_base::binary);
deque <char> v((istreambuf_iterator <char> (ifs)), istreambuf_iterator <char> ());
ifs.close();
v.erase(v.begin(), find_if(v.begin(), v.end(), bind2nd(not_equal_to <char> (), 0)));
ofstream ofs( "xxx ", ios_base::binary);
copy(v.begin(), v.end(), ostreambuf_iterator <char> (ofs));
[解决办法]
-_-!!! 我写了个C++的代码,可以实现你要的操作:

===================================================================
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

int main(int argc, char* *argv)
{
FILE* fp = fopen( "filename ", "r+t ");
FILE* nfp = fopen( "newfile ", "w+t ");

unsigned short buf = 0;

if( fp )
{
bool flag = false;
while( !feof(fp) )
{
fread(&buf,2,1,fp);
if( (buf & 0xFFFF) || flag )
fwrite(&buf,2,1,nfp);else
flag = true;
}
}
else
cout < < "file not exist! " < < endl;

return 1;
}

[解决办法]
试试用fstream读,然后做一个比较,如果是0那么什么也不做,否则存储在另一个文件中。

这样的话应该能实现

热点排行