呵呵, 这是我的第一篇blog
昨天在处理一个超大(2G多)log文件时碰到点问题。
首先, 想用ultraedit打开它,但由于c:盘太小,不能产生一个临时文件,
后用word, 提示内存太小,天啊, 我可是有1G的内存啊,
再到网上找了一个可以在windows下运行的tail命令(unxutils.sourceforge.net),
一试,居然提示不能打开此文件。
没法,看样子只能考自己写一个,其实也没什么算法,主要是对int64的操作。
下面就是程序,
欢迎大家使用,如果发现什么bug, 呵呵, 也别忘了告诉偶。
/**
* a utility to tail a large file > 2G
* develop in c++builder
* chenzero@netease.com
* 2006.8
*/
#pragma hdrstop
#include <windows.h>
//---------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <assert>
using namespace std;
#pragma argsused
string sss = string(78, char(2));
string ttt = string(78, char('~'));
void usage()
{
cout<<"Usage: tail filename <lines>"<<endl;
cout<<" Print the last N lines of the file to standard output."<<endl;
cout<<" This tool is developed to support big files."<<endl;
cout<<ttt<<endl;
cout<<"any comments, please contact at"<<endl;
cout<<"chenzero@netease.com"<<endl;
cout<<"2006.8"<<endl;
}
void SetSeekPos(__int64 p, LONG& h, LONG& l)
{
h = p>>32;
l = p&0x00000000FFFFFFFF;
}
int main(int argc, char* argv[])
{
DWORD ret;
if (argc<2) {
usage();
exit(0);
}
char* fileName=argv[1];
int line=10;
if (argc>=3) {
line = atoi(argv[2]);
if (line<1) {
line=10;
}
}
const int BUF_SIZE=1024;
HANDLE f = CreateFile(fileName, FILE_READ_DATA, FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (f==INVALID_HANDLE_VALUE) {
cerr << "canot open the file: "<< fileName <<endl;
return -1;
}
try {
BY_HANDLE_FILE_INFORMATION fileInfo;
ret=GetFileInformationByHandle(f, &fileInfo);
if (ret==FALSE) {
cerr<<"get file size error"<<endl;
return -1;
}
__int64 fileSize= fileInfo.nFileSizeHigh;
fileSize=fileSize<<32;
fileSize=fileSize|fileInfo.nFileSizeLow;
cout<<"size: " << fileSize<<endl;
cout<<"line: "<< line<<endl;
char buf[BUF_SIZE+1];
int nline = 0;
LONG ph,pl;
ret=SetFilePointer(f, 0, NULL, FILE_END);
if (ret==INVALID_SET_FILE_POINTER) {
cout<<"canot seek file pointer"<<endl;
return -1;
}
__int64 p = fileSize;
int readSize;
bool done=false;
bool first=true;
while (!done) {
if (p==0) {
break;
}
readSize = BUF_SIZE;
p = p - BUF_SIZE;
if (p<0) {
readSize=int(p+BUF_SIZE);
p=0;
}
SetSeekPos(p, ph,pl);
ret=SetFilePointer(f, pl, &ph, FILE_BEGIN);
if (ret==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR ) {
cout<<"canot seek file pointer"<<endl;
return -1;
}
DWORD bufRead=0;
ret=ReadFile(f,buf, readSize, &bufRead, NULL);
if (ret==FALSE) {
cout<<"read file error"<<endl;
return -2;
}
assert(bufRead>0);
for(int i=bufRead-1;i>=0;i--) {
if (buf[i]=='/0') {
cout<<"Error: it seems that the file "<<fileName << " isn't text file."<<endl;
cout<<" now, it's not supported"<<endl;
return -2;
}
if (first) {
nline++;
first=false;
}
else {
if (buf[i]=='/n') {
nline++;
}
}
if (nline>line) {
p = p + i+1;
done=true;
break;
}
}
}
SetSeekPos(p, ph,pl);
ret=SetFilePointer(f, pl, &ph, FILE_BEGIN);
if (ret==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR ) {
cout<<"canot seek file pointer"<<endl;
return -1;
}
cout<< sss<<endl;
buf[BUF_SIZE]='/0';
char lastChar=0;
while(true) {
DWORD bufRead=0;
ret=ReadFile(f,buf, BUF_SIZE, &bufRead, NULL);
if (ret==FALSE) {
cout<<"read file error"<<endl;
return -2;
}
if (bufRead==0) {
break;
}
buf[bufRead]='/0';
lastChar=buf[bufRead-1];
cout<< buf;
}
if (lastChar!='/n') {
cout<<endl;
}
cout<<sss<<endl;
return 0;
}
__finally {
CloseHandle(f);
}
}
//---------------------------------------------------------------------------