如果从文件的最后一行读起,往上读 我想从一个日志文件的最后一行读到,每读一行放进一变量里,到规定的行数就不再读了.用CFile还是用CStdioFile?还是有其它的方法。好像CFile有一个SeekToEnd(),但我不知怎么用..有人知道么? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 SeekToEnd()可以返回文件的字节数。你可以用它的返回值来定义数组。 SeekToEnd(),是把流指针设置到文件末尾不能满足你的从最后一行读取(如果每行大小相同倒是可以)没有办法能从最后一行往前读取还是从头读取,不需要的忽略。 最后我用C实现了这个功能.大概是先把指针放到最后,然后往上移一个字符,检查这个字符是不是"\n"如果不是则继续往上移,直到"\n",然后读取"\n"下的一行即可FILE * fp = fopen(logPath_,"r");//只读方式读取日志文件if(fp){ char ch; int nPos = -1; while( fseek( fp, nPos, SEEK_END) == 0 )//从文件尾开始读 { ch = fgetc( fp );//获取当前指针的字符 if( ch == '\n' )//当为回车时 { nPos ++;// fseek( fp, nPos, SEEK_END);//把指针往下放一个字符 fgets( strRet, sizeof( strRet ), fp );//在当前指针读取一行文本 // break; if (StrPosition(strRet,dealTypeName)>=0)//当找到相应交易类型时 { dealNumber++; if(StrPosition(strRet,"true")>=0)//当发现有超时的信息时 dealOver++; if(dealNumber==dealNum) { fclose( fp );//关闭文件流 return dealOver; } } nPos--; } nPos --;//再往上读一个字符 } } "栈"是在内存中,可是不符合从文件中读取的初衷了。可以考虑使用CList使用汇编就多得是"栈"了。如果要从文件中读取,可以创建一种自定义的文本型数据库。研究一下数据库原理,不会很难的。 #include "stdio.h"#include "assert.h"template <class ELEM> class Stack{ public: ELEM *ElmList; int top; int maxsize; Stack(int size); ~Stack(); Push(ELEM item); ELEM Pop(); ELEM GetTop(); bool IsEmpty(); bool IsFull(); };template <class ELEM>Stack<ELEM>::Stack(int size){ maxsize = size; ElmList = new ELEM[maxsize]; assert(ElmList != (ELEM *)NULL); top = -1;}template <class ELEM>Stack<ELEM>::~Stack(){ delete ElmList;}template <class ELEM>bool Stack<ELEM>::IsFull(){ return top == maxsize - 1;}template <class ELEM>bool Stack<ELEM>::IsEmpty(){ return top == -1;}template <class ELEM>Stack<ELEM>::Push(ELEM item){ assert(!IsFull()); top ++; ElmList[top] = item;}template <class ELEM>ELEM Stack<ELEM>::Pop(){ assert(!IsEmpty()); return ElmList[top --];}template <class ELEM>ELEM Stack<ELEM>::GetTop(){ assert(!IsEmpty()); return ElmList[top];}void main(){ Stack<int> s(10); s.Push(1); s.Push(2); s.Push(3); s.Push(4); s.Push(5); printf("%d ", s.Pop()); printf("%d ", s.Pop()); printf("%d ", s.Pop()); printf("%d ", s.Pop()); printf("%d ", s.Pop()); printf("\n\n");}这是一个放入数字的栈有没有人帮我改成可以放入字符串的,且不限制最大数的那一种(主要是因为很多应用都是不能直接限死你的最大size),本来我是想自己研究,但工作很忙就..... 用MFC做定时关机程序,当鼠标键盘无操作时自动关机,应该怎么做?如何得知鼠标键盘何时不再操作呢? glsl 及FBO 问题 求大牛解答 非常奇怪的函数调用现象,怀疑编译器错乱 MFC中如何用WM_Paint() 消息对pciture 控件画图 数据表间如何批量传输数据? ODBC连接方式的数据查询 网络上可以接开发任务赚钱吗? 能否在对话框中进行文本编辑??????? 谢谢 NT服务程序中如何修改线程的权限 关于COM 传递参数 用winpcap抓包,分析应用层时候遇到了问题 已知的C/S结构 我可否再把第三者放进去呢?
不能满足你的从最后一行读取(如果每行大小相同倒是可以)
没有办法能从最后一行往前读取还是从头读取,不需要的忽略。
大概是先把指针放到最后,然后往上移一个字符,检查这个字符是不是"\n"
如果不是则继续往上移,直到"\n",然后读取"\n"下的一行即可FILE * fp = fopen(logPath_,"r");//只读方式读取日志文件if(fp)
{
char ch;
int nPos = -1; while( fseek( fp, nPos, SEEK_END) == 0 )//从文件尾开始读
{
ch = fgetc( fp );//获取当前指针的字符
if( ch == '\n' )//当为回车时
{
nPos ++;//
fseek( fp, nPos, SEEK_END);//把指针往下放一个字符
fgets( strRet, sizeof( strRet ), fp );//在当前指针读取一行文本
// break;
if (StrPosition(strRet,dealTypeName)>=0)//当找到相应交易类型时
{
dealNumber++;
if(StrPosition(strRet,"true")>=0)//当发现有超时的信息时
dealOver++;
if(dealNumber==dealNum)
{
fclose( fp );//关闭文件流
return dealOver;
}
}
nPos--;
}
nPos --;//再往上读一个字符
}
}
使用汇编就多得是"栈"了。
如果要从文件中读取,可以创建一种自定义的文本型数据库。研究一下数据库原理,不会很难的。
#include "assert.h"
template <class ELEM> class Stack
{
public:
ELEM *ElmList;
int top;
int maxsize; Stack(int size);
~Stack(); Push(ELEM item);
ELEM Pop();
ELEM GetTop(); bool IsEmpty();
bool IsFull();
};template <class ELEM>
Stack<ELEM>::Stack(int size)
{
maxsize = size;
ElmList = new ELEM[maxsize];
assert(ElmList != (ELEM *)NULL);
top = -1;
}template <class ELEM>
Stack<ELEM>::~Stack()
{
delete ElmList;
}template <class ELEM>
bool Stack<ELEM>::IsFull()
{
return top == maxsize - 1;
}template <class ELEM>
bool Stack<ELEM>::IsEmpty()
{
return top == -1;
}template <class ELEM>
Stack<ELEM>::Push(ELEM item)
{
assert(!IsFull());
top ++;
ElmList[top] = item;
}template <class ELEM>
ELEM Stack<ELEM>::Pop()
{
assert(!IsEmpty());
return ElmList[top --];
}template <class ELEM>
ELEM Stack<ELEM>::GetTop()
{
assert(!IsEmpty());
return ElmList[top];
}void main()
{
Stack<int> s(10);
s.Push(1);
s.Push(2);
s.Push(3);
s.Push(4);
s.Push(5); printf("%d ", s.Pop());
printf("%d ", s.Pop());
printf("%d ", s.Pop());
printf("%d ", s.Pop());
printf("%d ", s.Pop());
printf("\n\n");
}
这是一个放入数字的栈
有没有人帮我改成可以放入字符串的,且不限制最大数的那一种(主要是因为很多应用都是不能直接限死你的最大size),本来我是想自己研究,但工作很忙就.....