C++ 读取文件的最后X字节
谁能告诉我一个简单的方法,如何读取特定文件的最后X字节?C++ 读取文件的最后X字节,c++,file-io,ifstream,C++,File Io,Ifstream,谁能告诉我一个简单的方法,如何读取特定文件的最后X字节? 如果我是对的,我应该使用ifstream,但我不知道如何使用它。目前我正在学习C++(至少我正在努力学习:))。 你需要使用He SekKa函数,并从流的结尾传递一个负偏移。 std::ifstream is("file.txt"); if (is) { is.seekg(-x, is.end); // x is the number of bytes to read before the end } 输入文件流具有将当前位置重
如果我是对的,我应该使用ifstream,但我不知道如何使用它。目前我正在学习C++(至少我正在努力学习:))。 你需要使用He<代码> SekKa函数,并从流的结尾传递一个负偏移。
std::ifstream is("file.txt");
if (is)
{
is.seekg(-x, is.end); // x is the number of bytes to read before the end
}
输入文件流具有将当前位置重新定位到绝对位置或相对位置的
seekg()
方法。一个重载采用表示绝对值的position类型。另一个采用偏移类型和方向遮罩,用于确定要移动到的相对位置。抵消偏移量可以使您向后移动。指定end
常量会相对于端点移动指示器
file.seekg(-x, std::ios_base::end);
这是一个C解决方案,但可以工作并处理错误。诀窍是在
fseek
中使用负索引“从EOF进行搜索”(即:从“右侧”进行搜索)
#包括
#定义基本单位大小(4096)
内部主(空){
int i;
const char*fileName=“test.raw”;
char buf[buf_SIZE]={0};
int字节读取=0;
文件*fp;/*输入文件的句柄*/
size\u t fileSize;/*输入文件的大小*/
int lastXBytes=100;/*要读取的文件末尾的字节数*/
/*以只读模式以二进制文件的形式打开文件*/
if((fp=fopen(“./test.txt”,“rb”))==NULL){
printf(“无法打开输入文件;正在中止\n”);
返回1;
}
/*找出文件的大小;重置指向文件开头的指针*/
fseek(fp,0L,SEEK_END);
fileSize=ftell(fp);
fseek(fp、0L、SEEK\u集);
/*确保文件足够大,可以读取最后的X字节数据*/
if(文件大小0){
对于(i=0;i#包括
#包括
使用名称空间std;
int main(int argc,char*argv)
{
ifstream-ifs(“F:\\test.data”,ifstream::binary);
if(ifs.fail())
{
cout用于文件末尾的相对定位,然后使用:
ifstream-ifs(“test.txt”);
int x=10;
字符缓冲区[11]={};
如果seekg(-x,ios_base::end);
如果(!ifs.read(缓冲区,x))
cerr您需要尽可能多地完成。然后向我们展示您的代码并寻求帮助。请查看seekg
#include <stdio.h>
#define BUF_SIZE (4096)
int main(void) {
int i;
const char* fileName = "test.raw";
char buf[BUF_SIZE] = { 0 };
int bytesRead = 0;
FILE* fp; /* handle for the input file */
size_t fileSize; /* size of the input file */
int lastXBytes = 100; /* number of bytes at the end-of-file to read */
/* open file as a binary file in read-only mode */
if ((fp = fopen("./test.txt", "rb")) == NULL) {
printf("Could not open input file; Aborting\n");
return 1;
}
/* find out the size of the file; reset pointer to beginning of file */
fseek(fp, 0L, SEEK_END);
fileSize = ftell(fp);
fseek(fp, 0L, SEEK_SET);
/* make sure the file is big enough to read lastXBytes of data */
if (fileSize < lastXBytes) {
printf("File too small; Aborting\n");
fclose(fp);
return 1;
} else {
/* read lastXBytes of file */
fseek(fp, -lastXBytes, SEEK_END);
bytesRead = fread(buf, sizeof(char), lastXBytes, fp);
printf("Read %d bytes from %s, expected %d\n", bytesRead, fileName, lastXBytes);
if (bytesRead > 0) {
for (i=0; i<bytesRead; i++) {
printf("%c", buf[i]);
}
}
}
fclose(fp);
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv)
{
ifstream ifs("F:\\test.data", ifstream::binary);
if(ifs.fail())
{
cout << "Error:fail to open file" << endl;
return -1;
}
//read the last 10 bits of file
const int X = 10;
char* buf = new char[X];
ifs.seekg(-X, SEEK_END);
ifs.read(buf, X);
ifs.close();
delete buf;
return 0;
ifstream ifs("test.txt");
int x=10;
char buffer[11]={};
ifs.seekg(-x, ios_base::end);
if (!ifs.read(buffer, x))
cerr << "There's a problem !\n";
else cout <<buffer<<endl;