我存储了二进制文件中的有用数据,我需要将其添加到结构中,直到出现垃圾数据(当第一个垃圾位出现时我停止将数据加载到结构中)。我需要将文件中的那些垃圾数据(我没有存储到结构中)写入(复制)到另一个文件。如何在不为垃圾数据分配数组的情况下使用f1 . read ( junkArray, junkLen )
然后使用
file . write ( junkArray , junkLen ) ;
我想做点什么
file . write ( f1 , junkLen );
其中f1是垃圾数据开始的位置文件。
答案 0 :(得分:0)
网站cplusplus有一个典型的例子,说明如何在特定位置搜索,read和write - 阅读它是这样的
// read a file into memory
#include <iostream> // std::cout
#include <fstream> // std::ifstream
int main () {
std::ifstream is ("test.txt", std::ifstream::binary);
if (is) {
// get length of file:
is.seekg (0, is.end);
int length = is.tellg();
is.seekg (0, is.beg);
// allocate memory:
char * buffer = new char [length];
// read data as a block:
is.read (buffer,length);
is.close();
// print content:
std::cout.write (buffer,length);
delete[] buffer;
}
return 0;
}
只需将seekg
中的参数替换为您实际想要读/写的位置。
写作时,请查看seekp