首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

线程读取文件的有关问题

2012-02-25 
线程读取文件的问题#includewindows.h #includeutility#includestring#includefstream#includeio

线程读取文件的问题
#include   "windows.h "  
#include   <utility>  
#include   <string>
#include   <fstream>
#include   <iostream>
using   namespace   std;
//#include   <ansi_c.h>  


DWORD   id;  
HANDLE   ch;  

int   chechen;  

void   Check_Time(void);  


SYSTEMTIME   ti;  

/*  
typedef   struct   _SYSTEMTIME   {  
WORD   wYear;  
WORD   wMonth;  
WORD   wDayOfWeek;  
WORD   wDay;  
WORD   wHour;  
WORD   wMinute;  
WORD   wSecond;  
WORD   wMilliseconds;  
}   SYSTEMTIME,   *PSYSTEMTIME;  
*/  


void   main(void)  
{  
chechen   =   0;  
ch   =   CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Check_Time,0,0,&id);  
WaitForSingleObject(ch,   INFINITE);  
}  
void   Check_Time(void)  
{  
char   *tim,   time[100];  
int   i;  

/////////////////////////////////////////////////////////////////////////////
char   *   file= "C:\\Program   Files\\Gene6   FTP   Server\\Log\\emma-transfers.log ";
        ifstream   ifs;
ifs.open(file,ios::binary);//open   file   the   file   name   can   be   replaced
ifs.seekg   (0,   ios::end);
int   dwFilePosition=ifs.tellg();
int   length=0;
ifs.seekg(0,ios::beg);
string   str;
ifs.close();
char   buf[1024];


//////////////////////////////////////////////////////////////////////////////

while   (1)  
{  
Sleep(5000);  

ifs.open( "C:\\Program   Files\\Gene6   FTP   Server\\Log\\emma-transfers.log ",ios::binary);//open   file   the   file   name   can   be   replaced
ifs.seekg(0,ios::end);
length=ifs.tellg();
ifs.seekg(0,ios::beg);
int   newadded=length-dwFilePosition;
cout < < "length:       " < <length < <endl;
ifs.seekg   (-newadded,   ios::end);   //added   newadded   bytes




if   (length <dwFilePosition)
{
ifs.getline(buf,100, '\n ');
cout < <buf < < "no   changed " < <endl;
cout < <length < < "   this   is   length " < <endl;
cout < <dwFilePosition < < "   position " < <endl;
//The   file   has   rolled   over...   (or   it 's   been   cleared)


}
else   if(length==dwFilePosition)
{}

else   if(length> dwFilePosition)
{
memset(buf,0,1024);  
ifs.getline(buf,100, '\n ');

//ifs.getline(buf,100, '\n ');
cout < <buf < < "changed " < <endl;
///if   the   file   has   been   changed,   it   should   notify   the   program   to   print   the  
//newly   added   lines
//and   continue

cout < <length < < "   this   is   length " < <endl;


cout < <dwFilePosition < < "   position " < <endl;
dwFilePosition=length;
}
ifs.close();

}  
}


下面是输出结果:
length:       10742
length:       10742
length:       10742
length:       10742
length:       10742
length:       10742
length:       10742
length:       10861
07/02/08   15:12:05,   10,   127.0.0.1,   liuxiaoyun,   STOR   E:\ftphome\sn.txt   from   0   to   1
24   in   00:00:01   at   0changed
10861   this   is   length
10742   position
length:       -1
no   changed
-1   this   is   length
10861   position
length:       -1
no   changed
-1   this   is   length
10861   position
length:       -1


为什么下面的length在文件长度改变后变成了-1呢?

[解决办法]
帮顶
[解决办法]
basic_istream::getline "The function extracts up to _Count - 1 elements and stores them in the array beginning at _Str "

You can get ride of this problem by adding following after "ifs.getline(buf,100, '\n ') "

if (ifs.fail()) ifs.clear();

热点排行