[C] 纯文本查看 复制代码
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <windows.h>
int thisNow(int i);
int shenYing(void);
void shenPing(void);
int main(void)
{
//system("mode con: cols=150 lines=30");//设置窗口大小
system("mode con: cols=50 lines=30");
system("color 1f");
//printf("老钟\n\n");
FILE *fp;
if((fp=fopen("log.txt","wb"))==NULL)
{
printf("\nopen file fail,close!");
getchar();
exit(0);
}
// int thSY;
// for(thSY=0;thSY<10;thSY++)
// {
// printf("\a");
// }
//MessageBeep(MB_OK);//api输出声音
//shenYing();
/*
Sleep(1000);
MessageBeep(0xFFFFFFFF);//API函数前加“::”符号,表示这是一个全局的函数,以与c++类的成员函数相区分
Sleep(1000);
MessageBeep(MB_ICONASTERISK);
Sleep(1000);
MessageBeep(MB_ICONEXCLAMATION);
Sleep(1000);
MessageBeep(MB_ICONHAND);
Sleep(1000);
MessageBeep(MB_ICONQUESTION);
Sleep(1000);
MessageBeep(MB_OK);
Sleep(1000);
*/
//fprintf(fp,myLog);
fputs("\n=============勇哥网络简单检测程序V1.0=============\n",fp);
fclose(fp);
pingStart:
printf("\n=============勇哥网络简单检测程序V1.0=============\n");
printf("\n===============隔10分钟自动检测网络===============\n");
printf("\n==============详细请查看日志log.txt===============\n");
printf("\n现在时间是: ");
thisNow(0);
int lineCount=getNumberOfEdges();//IP总行数
char ip[lineCount][15];
char buf[1024]; /*缓冲区*/
FILE *fpIP; /*文件指针*/
int len; /*行字符个数*/
int numofline;
numofline=0;
if((fpIP = fopen("pingIP.txt","r"))== NULL)
{
perror("fail to read");
exit (1) ;
}
while(fscanf(fpIP, "%s", &ip[numofline])!= EOF) //读取数据到数组,直到文件结尾(返回EOF)
{
//printf("ip[%d]=%s\n",numofline,&ip[numofline]);//输出值看下对不对
numofline++;
}
//int zongNum=sizeof(ip)/sizeof(ip[0]);//数组个数
printf("\n检测的IP地址 %d 个,分别为:\n",numofline);
int numGe=0;
for(numGe=0;numGe<numofline;numGe++)
{
printf("%d.%s \n",numGe+1,&ip[numGe]);
}
printf("\n");
for(numGe=0;numGe<numofline;numGe++)
{
char *myChar=ip[numGe];
char myPing[20]="ping ";
strcat(myPing,myChar);
//printf("%s",myPing);
int res;
res=system(myPing);
if(res==0)
{
printf("\n------------------->>>成功!<<<-------------------\n");
}
else
{
shenYing();//网不通声音提示
shenPing();//屏幕变色
shenYing();
shenPing();
printf("\n------------->>>失败,请检查日志!<<<-------------\n");
char *myLog=ip[numGe];
char myT[100]=" ---网络不通--->>> ";
strcat(myT,myLog);
thisNow(1);
writeLog(myT);
system("cls");
}
}
system("mode con: cols=50 lines=5");
//Sleep(600000);//等待时间秒
int thisMiao;
for(thisMiao=0;thisMiao<10;thisMiao++)
{
printf("\n现在时间:");
thisNow(0);
printf("\n-------->>>等待,%d秒后重新检测网络!<<<---------",10-thisMiao);
Sleep(1000);
system("cls");
}
system("mode con: cols=50 lines=30");
goto pingStart;
return 0;
}
//显示日期函数
int thisNow(int i)
{
char *wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
time_t timep;
struct tm *p;
time(&timep);
//p = gmtime(&timep);
p = localtime(&timep);
if(i==0)
{
printf(" %d年%d月%d日 ", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday);
printf("%s %d:%d:%d\n", wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
}
else
{
FILE *fp;
if((fp=fopen("log.txt","a"))==NULL)
{
printf("\nopen file fail,close!");
getchar();
exit(0);
}
//fprintf(fp,myLog);
fprintf(fp,"\n\n%d年%d月%d日 ", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday);
fprintf(fp,"%s %d:%d:%d", wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
fflush(fp); //数据刷新 数据立即更新
fclose(fp);
}
}
//网络不通时输出日志
int writeLog(char wlog[])
{
FILE *fp;
//if((fp=fopen("log.txt","wb"))==NULL)
if((fp=fopen("log.txt","a"))==NULL)
{
printf("\nopen file fail,close!");
getchar();
exit(0);
}
fprintf(fp,wlog);
//fputs(wlog,fp);
fflush(fp); //数据刷新 数据立即更新
fclose(fp);
}
int shenYing(void)
{
int m[]={0,320,391,399,445,466,494, 523}; //定义曲谱中的 do rui mi fa sou la ci 不准确 自己靠感觉调的
int s[]={5,3,5,3,5,2,1}; //我是一个粉刷匠 的曲谱 我就会这句。。。
int t[]={300,300,300,300,300,300,500}; //这是每个音的发生时间, 可以自己定义 半拍多少时间 四分之一拍多少时间
int n;
for (n=0; n<7; n++)
{
Beep(m[s[n]],t[n]); //Beep函数 第一个参数 是 发生频率 第二个参数 是发声时间
}
return 0;
}
//屏幕颜色变换
void shenPing(void)
{
Sleep(300);
system("color 0f");
Sleep(300);
system("color 1f");
Sleep(300);
system("color 2f");
Sleep(300);
system("color 3f");
Sleep(300);
system("color 4f");
Sleep(300);
system("color 5f");
Sleep(300);
system("color 6f");
Sleep(300);
system("color 7f");
Sleep(300);
system("color 8f");
Sleep(300);
system("color 9f");
Sleep(300);
system("color 1f");
}
//计算txt行数
int getNumberOfEdges()
{
char buf[1024]; /*缓冲区*/
FILE *fp; /*文件指针*/
int len; /*行字符个数*/
int numofline;
numofline=0;
if((fp = fopen("pingIP.txt","r")) == NULL)
{
perror("fail to read");
exit (1) ;
}
while(fgets(buf,1024,fp) != NULL)
{
numofline++;
len = strlen(buf);
buf[len-1] = '\0'; /*去掉换行符*/
//printf("%s %d \n",buf,len - 1);
}
//printf("行数: %d \n",numofline);
return numofline;
}