C++ 如何制作';阅读';函数是否从文件的开头开始?
我试图读取一个二进制文件,我正在使用C++ 如何制作';阅读';函数是否从文件的开头开始?,c++,C++,我试图读取一个二进制文件,我正在使用f_in.read((char(*)和tmp,sizeof(tmp))函数。但是,每次调用此函数时,它都会从上一个读取函数停止的位置继续读取文件。是否可以使读取函数每次调用时都从文件的开头开始 打开pixmap.bin文件: int main(){ ifstream f_in; f_in.open("Pixmap.bin", ios::binary); if (f_in.fail()) { cerr<<
f_in.read((char(*)和tmp,sizeof(tmp))
函数。但是,每次调用此函数时,它都会从上一个读取函数停止的位置继续读取文件。是否可以使读取函数每次调用时都从文件的开头开始
打开pixmap.bin文件:
int main(){
ifstream f_in;
f_in.open("Pixmap.bin", ios::binary);
if (f_in.fail()) {
cerr<<"Error while opening the file pixmap.bin"<<endl;
f_in.close();
exit(EXIT_FAILURE);
}
intmain(){
如果流f_in;
f_in.open(“Pixmap.bin”,ios::binary);
如果(f_in.fail()){
cerr这与文件指针有关,请尝试在“文件指针”部分阅读此页:
下面的示例给出:
int main()
{
int x;
streampos pos;
ifstream infile;
infile.open("silly.dat", ios::binary | ios::in);
infile.seekp(243, ios::beg); // move 243 bytes into the file
infile.read(&x, sizeof(x));
pos = infile.tellg();
cout << "The file pointer is now at location " << pos << endl;
infile.seekp(0,ios::end); // seek to the end of the file
infile.seekp(-10, ios::cur); // back up 10 bytes
infile.close();
}
intmain()
{
int x;
streampos;
河流充填;
infle.open(“silly.dat”,ios::binary | ios::in);
seekp(243,ios::beg);//将243字节移动到文件中
填充读取(&x,sizeof(x));
pos=infie.tellg();
您需要发布其他人可以运行的代码。我猜f\u n
是(i)fstream
?如果是请参见kg
work@Mandralis书签。从学习基本的文件I/O概念和术语开始,应该会有所帮助。为什么要多次读取相同的数据?
int main()
{
int x;
streampos pos;
ifstream infile;
infile.open("silly.dat", ios::binary | ios::in);
infile.seekp(243, ios::beg); // move 243 bytes into the file
infile.read(&x, sizeof(x));
pos = infile.tellg();
cout << "The file pointer is now at location " << pos << endl;
infile.seekp(0,ios::end); // seek to the end of the file
infile.seekp(-10, ios::cur); // back up 10 bytes
infile.close();
}