当前位置:网站首页>Seekg, tellg related file operations
Seekg, tellg related file operations
2022-06-12 13:30:00 【Boring ah le】
seekg() And tellg() Related file operations
Operate on the input stream :seekg() And tellg()
Operate on the output stream :seekp() And tellp()
The following uses the input stream function as an example :
seekg() Is to locate the input file , It has two parameters : The first parameter is the offset , The second parameter is the base address .
For the first parameter , It can be a positive or negative value , Positive indicates a backward offset , Negative indicates forward offset . The second parameter can be :
ios::beg: Indicates the starting position of the input stream
ios::cur: Indicates the current position of the input stream
ios::end: Indicates the end position of the input stream
tellg() The function does not need to take arguments , It returns the position of the current positioning pointer , It also represents the size of the input stream .
The procedure is :
#include <iostream>
#include <fstream>
#include <assert.h>
using namespace std;
int main()
{
ifstream in("test.txt");
assert(in);
in.seekg(0,ios::end); // The base address is at the end of the file , The offset address is 0, So the pointer is at the end of the file
streampos sp=in.tellg(); //sp Position pointer for , Because it's at the end of the file , So that's the size of the file
cout<<"file size:"<<endl<<sp<<endl;
in.seekg(-sp/3,ios::end); // The base address is the end of the file , The offset address is negative , So move forward sp/3 Bytes
streampos sp2=in.tellg();
cout<<"from file to point:"<<endl<<sp2<<endl;
in.seekg(0,ios::beg); // Base address is file header , The offset for the 0, So it is located in the file header
cout<<in.rdbuf(); // Read the contents of the file from the beginning
in.seekg(sp2);
cout<<in.rdbuf()<<endl; // from sp2 Start reading the contents of the file
return 0;
}
int main()
{
// Get the file size :C++ The way
ifstream ifs;
ifs.open("log.txt");
assert(ifs.is_open());
ifs.seekg( 0 , std::ios::end );
cout<<ifs.tellg()<<endl;
ifs.close();
// Get the file size :C The way
FILE* fp = fopen("log.txt", "rb");
assert ( 0 == fseek(fp, 0, SEEK_END));
unsigned int usize = ftell(fp);
cout<<usize<<endl;
fclose(fp);
return 0;
}
边栏推荐
- 章鱼网络进展月报 | 2022.5.1-5.31
- torch_geometric message passing network
- "New continent" of mobile application going to sea
- 开发中使用的语言技巧
- 【刷题篇】超级洗衣机
- 442 authors, 100 pages! It took Google 2 years to release the new benchmark big bench | open source
- Script引入CDN链接提示net::ERR_FILE_NOT_FOUND问题
- Implementing tensorflow deep learning framework similarflow with numpy
- Summary of question brushing in leetcode sliding window
- import torch_geometric 第一个图网络例子
猜你喜欢

Installation of pagoda

Stm32f1 and stm32cubeide programming examples - device driver -eeprom-at24c256 driver

leetcode 47. Permutations II full permutations II (medium)

import torch_ Data view of geometric

D1 哪吒开发板 了解基本的启动加载流程

Overview of embedded system 2- composition and application of embedded system

Paw 高级使用指南

Title: Yanghui triangle

5V升压到12.6V的锂电池充电IC芯片方案FS4062B

"New continent" of mobile application going to sea
随机推荐
"Non" reliability of TCP
[Title brushing] Super washing machine
How to balance multiple losses in deep learning?
1005: estimation of the earth's population carrying capacity
C language implementation of string and memory library functions
Getting started with NVIDIA Jetson nano Developer Kit
Volume mount and mirror creation
[brush title] probability of winning a draw
STM32F1与STM32CubeIDE编程实例-设备驱动-EEPROM-AT24C256驱动
JVM 运行时参数
Symbolic constant, const qualifier
hudi 键的生成(Key Generation)
[cloud native | kubernetes] learn more about ingress
手把手教你IDEA创建SSM项目结构
AVFoundation
高通平台开发系列讲解(协议篇)QMI简单介绍及使用方法
单向环形链表实现约瑟夫环
【云原生 | Kubernetes篇】深入了解Ingress
2068: [example 2.6] chicken and rabbit in the same cage
leetcode 47. Permutations II 全排列 II(中等)