如何让'read'函数从文件的开头开始?
How to make the 'read' function start from the beginning of the file?
我正在尝试读取一个二进制文件,并且正在使用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);
}
我想每次从头开始读取时使用的功能:
void Read_Dimensions(ifstream &f_in, int Dimensions[2]) {
uint tmp(0);
for(int i=0; i<2;i++) {
f_in.read((char*) &tmp, sizeof(tmp));
Dimensions[i]=tmp;
}
}
这是相对于文件指针的,请尝试在"文件指针"部分中读取此页:http://www.eecs.umich.edu/courses/eecs380/HANDOUTS/cppBinaryFileIO-2.html
这里的例子给出:
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();
}
希望对你有所帮助。
相关文章:
- 当回溯以零开始时,如何调试崩溃
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 根据用户输入用字母填充矢量,并将"开始"和"结束"放在四肢
- 如何从头开始创建 avi 文件?
- 为什么 -mmacosx-version-min=10.10 不阻止使用标记为从 10.11 开始的函数?
- 自一周开始以来的秒数?
- 一旦双簧管录制开始,主 UI 线程就会被阻止
- 在 C++11 中,如何查找并返回以给定字符串开头的字符串向量中的所有项?
- GStreamer在开始任何播放之前进行搜索
- 字符串开头的分隔符
- 使 std::fstream 写入文件末尾,但从头开始读取
- 设计将引用元素移动到开头的数据结构.C++
- 如何使字符串出现在编译的二进制可执行文件的开头?
- 为什么 std::next 在以空地图开头馈送时卡住
- g++ 如何对齐内存以确保对齐从缓存行的开头开始?
- 从偏移量开始复制到结构的开头
- 如何让'read'函数从文件的开头开始?
- Qt - 如何设置音频播放从缓冲区的开头开始
- 从链表的末尾开始读取值,而不是从开头
- 如何在 ofn.lpstrFilter 中用数字开始文件格式名称?它不允许在开头写 3?