关于文件的读写操作,请教大家
一批文件 大小都是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那么什么也不做,否则存储在另一个文件中。
这样的话应该能实现