user-0 提问: 怎么显示最新的日志信息

回复:
user: user-1
content: 参考10楼相关API

user: user-1
content: 你还可以参考10楼代码，在文件有更新的时候，读取文件末尾的新信息。

user: user-0
content: 谢谢赵4老师，现在我是不断地打开关闭，把日志文件读出来。。。

user: user-1
content: 不能。  FindFirstChangeNotification The FindFirstChangeNotification function creates a change notification handle and sets up initial change notification filter conditions. A wait on a notification handle succeeds when a change matching the filter conditions occurs in the specified directory or subtree.   HANDLE FindFirstChangeNotification(   LPCTSTR lpPathName,    // pointer to name of directory to watch   BOOL bWatchSubtree,    // flag for monitoring directory or                           // directory tree   DWORD dwNotifyFilter   // filter conditions to watch for );   Parameters lpPathName  Pointer to a null-terminated string that specifies the path of the directory to watch.  bWatchSubtree  Specifies whether the function will monitor the directory or the directory tree. If this parameter is TRUE, the function monitors the directory tree rooted at the specified directory; if it is FALSE, it monitors only the specified directory.  dwNotifyFilter  Specifies the filter conditions that satisfy a change notification wait. This parameter can be one or more of the following values: Value Meaning  FILE_NOTIFY_CHANGE_FILE_NAME   Any filename change in the watched directory or subtree causes a change notification wait operation to return. Changes include renaming, creating, or deleting a filename.  FILE_NOTIFY_CHANGE_DIR_NAME   Any directory-name change in the watched directory or subtree causes a change notification wait operation to return. Changes include creating or deleting a directory.  FILE_NOTIFY_CHANGE_ATTRIBUTES   Any attribute change in the watched directory or subtree causes a change notification wait operation to return.  FILE_NOTIFY_CHANGE_SIZE   Any file-size change in the watched directory or subtree causes a change notification wait operation to return. The operating system detects a change in file size only when the file is written to the disk. For operating systems that use extensive caching, detection occurs only when the cache is sufficiently flushed.  FILE_NOTIFY_CHANGE_LAST_WRITE   Any change to the last write-time of files in the watched directory or subtree causes a change notification wait operation to return. The operating system detects a change to the last write-time only when the file is written to the disk. For operating systems that use extensive caching, detection occurs only when the cache is sufficiently flushed.  FILE_NOTIFY_CHANGE_SECURITY   Any security-descriptor change in the watched directory or subtree causes a change notification wait operation to return.    Return Values If the function succeeds, the return value is a handle to a find change notification object.  If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.   Remarks The wait functions can monitor the specified directory or subtree by using the handle returned by the FindFirstChangeNotification function. A wait is satisfied when one of the filter conditions occurs in the monitored directory or subtree.   After the wait has been satisfied, the application can respond to this condition and continue monitoring the directory by calling the FindNextChangeNotification function and the appropriate wait function. When the handle is no longer needed, it can be closed by using the FindCloseChangeNotification function.   QuickInfo   Windows NT: Requires version 3.1 or later.   Windows: Requires Windows 95 or later.   Windows CE: Unsupported.   Header: Declared in winbase.h.   Import Library: Use kernel32.lib.   Unicode: Implemented as Unicode and ANSI versions on Windows NT.  See Also File I/O Overview, File Functions, FindCloseChangeNotification, FindNextChangeNotification

user: user-0
content: 引用 7 楼 zhao4zhong1 的回复:仅供参考：#include <stdio.h> FILE *f; int c; long offset; char ln[100]; void main() {     f=fopen("a.txt","rb");//必须用b方式打开，否则文件当前读位置会受\x0d \x0a \x1a等字符的干扰     fseek(f,-1,SEEK_END);//倒数最后一个字节之前     while (1) {         c=fgetc(f);         if ('\n'==c) {             offset=ftell(f);//记住当前位置，即刚读字符后面             fgets(ln,100,f);             printf("%s",ln);             fseek(f,offset-2,SEEK_SET);//回到刚才读位置之前一个位置，即c所在位置之前一个位置         } else             if (fseek(f,-2,SEEK_CUR)) {//回到刚才读位置之前一个位置，即c所在位置之前一个位置失败，说明已读到文件开头                 fseek(f,0,SEEK_SET);                 fgets(ln,100,f);//读文件第一行内容                 printf("%s",ln);                 break;             }     }     fclose(f); }   只打开一次，能读取文件不断更新的内容吗？

user: user-2

user: user-1
content: 仅供参考：#include <stdio.h> FILE *f; int c; long offset; char ln[100]; void main() {     f=fopen("a.txt","rb");//必须用b方式打开，否则文件当前读位置会受\x0d \x0a \x1a等字符的干扰     fseek(f,-1,SEEK_END);//倒数最后一个字节之前     while (1) {         c=fgetc(f);         if ('\n'==c) {             offset=ftell(f);//记住当前位置，即刚读字符后面             fgets(ln,100,f);             printf("%s",ln);             fseek(f,offset-2,SEEK_SET);//回到刚才读位置之前一个位置，即c所在位置之前一个位置         } else             if (fseek(f,-2,SEEK_CUR)) {//回到刚才读位置之前一个位置，即c所在位置之前一个位置失败，说明已读到文件开头                 fseek(f,0,SEEK_SET);                 fgets(ln,100,f);//读文件第一行内容                 printf("%s",ln);                 break;             }     }     fclose(f); }

user: user-0
content: 写日志不是我控制的，我只是想把已经生成的日志文件，不断刷新到我的界面上。

user: user-3
content: 你的日志文件是由你控制吗？如果是，建议生成的日志写一份至文件，同时输出一个份至标准输出端； 如果不是，可以打开一次，然后保留文件的读位置，同时获得日志文件的当前大小，通过比较读的位置和当前的大小；若文件当前大小大于读的位置，就去文件里读，然后输出到终端。

user: user-4
content: 让你的Logger自己实现 同时向文件 和  向控制台 输出  如果你控制不了这个Logger的实现， 那就只能像你问题里面写的那样，每次都去读文件，读取最后n行，然后输出

user: user-5
content: 引用 2 楼 zhxingway 的回复:[quote=引用 1 楼 user-5 的回复:] 如果有写日志的程序源码，直接改写程序 写日志不一样，打开一次文件后用fflush写进去的。  因为我这个是要跨平台的，所以不能直接用tail的代码[/quote] 你的意思是有源码。那就这样改写：写文件的同时，同时向标准输出也写一份。 也可以不直接写文件，先放内存，用atexit注册回调函数，程序退出时写入文件。

user: user-0
content: 引用 1 楼 user-5 的回复:如果有写日志的程序源码，直接改写程序 写日志不一样，打开一次文件后用fflush写进去的。  因为我这个是要跨平台的，所以不能直接用tail的代码

user: user-5
content: 如果有写日志的程序源码，直接改写程序