#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <IOSTREAM.H>
#include <Windows.h>
#include <process.h>
bool CheckLastRate(char *lpContent);
unsigned int _stdcall Check_Thread(LPTSTR lpContent);
int ProcessRate = 0;
int main()
{
FILE *stream;
int ch;
int i = 0;
char content[1000];
HANDLE hHandle = NULL;
memset(content,0,1000);
stream = fopen("d:\\WIMrepairRead.txt","r");
fseek(stream, 0, SEEK_SET);
do
{
ch = fgetc(stream);
content[i] = ch ;
i++;
}
while (ch != EOF);
cout<<content<<endl;
fclose(stream);
hHandle = (HANDLE)_beginthreadex(NULL,0,(unsigned int (__stdcall *)(void *))Check_Thread,content,0,NULL);
WaitForSingleObject(hHandle,INFINITE );
printf("\nsucceed");
return 0;
}
bool CheckLastRate(char *lpContent)
{
/*cout<<"lpContent:"<<lpContent<<endl;*/
int iIndex = 0;
int iLastPercentPosition = 0;
int iPercentNum = 0;
for (;iIndex<strlen(lpContent);iIndex++)
{
if (lpContent[iIndex]=='%')
{
iPercentNum++;
iLastPercentPosition = iIndex ;
}
}
if (iPercentNum!=0) //有百分号的情况
{
//是两位数或三位数的情况
if (lpContent[iLastPercentPosition-2]>48 && lpContent[iLastPercentPosition-2]<=57)
{
ProcessRate = (lpContent[iLastPercentPosition-2]-'0')*10+(lpContent[iLastPercentPosition-1]-'0');
if (ProcessRate == 0) //三位数的情况
{
ProcessRate = 100;
}
}
//是一位数的情况
else
{
ProcessRate = lpContent[iLastPercentPosition-1]-'0';
}
cout<<ProcessRate<<endl;
return true;
}
else
{
return false;
}
}
unsigned int _stdcall Check_Thread(LPTSTR lpContent)
{
if(CheckLastRate(lpContent))
{
cout<<endl<<"发现"<<endl;
}
else
{
cout<<endl<<"no发现"<<endl;
}
Sleep(10000);
return 0;
}
对比下一段代码
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <IOSTREAM.H>
#include <Windows.h>
#include <process.h>
bool CheckLastRate(char *lpContent);
unsigned int _stdcall Check_Thread(LPTSTR lpContent);
int ProcessRate = 0;
int main()
{
FILE *stream;
int ch;
int i = 0;
char content[1000];
DWORD dwRet = (DWORD)-1;
HANDLE hHandle = NULL ;
memset(content,0,1000);
stream = fopen("d:\\WIMrepairRead.txt","r");
fseek(stream, 0, SEEK_SET);
do
{
ch = fgetc(stream);
content[i] = ch ;
i++;
}
while (ch != EOF);
cout<<content<<endl;
fclose(stream);
dwRet = _beginthreadex(NULL,0,(unsigned int (__stdcall *)(void *))Check_Thread,content,0,NULL);
if (dwRet==(DWORD)-1)
{
cout<<"failed"<<endl;
}
else
{
cout<<"succeed"<<endl;
}
//WaitForSingleObject(hHandle,INFINITE );
printf("\nsucceed");
return 0;
}
bool CheckLastRate(char *lpContent)
{
/*cout<<"lpContent:"<<lpContent<<endl;*/
int iIndex = 0;
int iLastPercentPosition = 0;
int iPercentNum = 0;
for (;iIndex<strlen(lpContent);iIndex++)
{
if (lpContent[iIndex]=='%')
{
iPercentNum++;
iLastPercentPosition = iIndex ;
}
}
if (iPercentNum!=0) //有百分号的情况
{
//是两位数或三位数的情况
if (lpContent[iLastPercentPosition-2]>48 && lpContent[iLastPercentPosition-2]<=57)
{
ProcessRate = (lpContent[iLastPercentPosition-2]-'0')*10+(lpContent[iLastPercentPosition-1]-'0');
if (ProcessRate == 0) //三位数的情况
{
ProcessRate = 100;
}
}
//是一位数的情况
else
{
ProcessRate = lpContent[iLastPercentPosition-1]-'0';
}
cout<<ProcessRate<<endl;
return true;
}
else
{
return false;
}
}
unsigned int _stdcall Check_Thread(LPTSTR lpContent)
{
if(CheckLastRate(lpContent))
{
cout<<endl<<"发现"<<endl;
}
else
{
cout<<endl<<"no发现"<<endl;
}
Sleep(10000);
return 0;
}