C批量读取wget的下载日志中的信息

  • 时间:
  • 来源:互联网
  • 文章标签:
      最近使用 wget 进行文件下载实验,由于数据下载量和次数比较大,手动的去打开log文件查看下载过程信息太浪费时间且易出错,故用 C 写了个小工具,批量的读取log文件夹中的信息。如下图是log文件夹中文件的命名规范。

在这里插入图片描述

      下图是 wget log文件的内容格式
--2020-12-18 00:02:47--  http://192.168.1.4:5555/file/64K
Connecting to 192.168.1.4:5555... connected.
HTTP request sent, awaiting response... 200 OK
Length: 65536 (64K) [application/octet-stream]
Saving to: ‘64K’

     0K .......... .......... .......... .......... .......... 78% 99.4K 0s
    50K .......... ....                                       100%  424K=0.5s

2020-12-18 00:02:48 (119 KB/s) - ‘64K’ saved [65536/65536]
      由于我只需要获取文件的下载时间信息,所以可以通过定位字符 ‘=’ 和 's’来获取时间。这是一种简单的方法,想起他的信息,比如下载速度,可以通过定位字符 'B’和其前面的第一个 '('来获取,注意网速的单位(KB/s、MB/s)。完整代码如下.
#include <stdio.h>
#include <string.h>

void calculation(char* choice);
double readFileData(char *file);

int main(void)
{
    char choices[5][15] = {"m","pr","rr","sctc","s"};
    int i = 0;
    for (i = 0; i < 5; i++)
    {
        calculation(choices[i]);
    }
    return 0;
}

void calculation(char* choice)
{
    const int count = 10;
    char file[10] = "",choice_t[15] = "";
    int i = 0;
    double max = -1,min = 0xffff,sum = 0,average = 0;
    for (i = 1;i < (count + 1); i++)
    {
        strcpy(choice_t,choice);
        file[0] = '-';
        if (i==10) file[1] = 'A';
        else file[1] = '0' + i;
        file[2] = '.';
        file[3] = 'l';
        file[4] = 'o';
        file[5] = 'g';
        file[6] = '\0';
        strcat(choice_t,file);
        double num = readFileData(choice_t);
        printf("%lf\n",num);
    }
}
// search the complete time of download by the file name
double readFileData(char *fileStr)
{
    FILE *file = fopen(fileStr,"r");
    //printf("%s\n",fileStr);
    if (NULL==file)
    {
        printf("file read error\n");
    }    
    char ch = 0;
    double result = 0;
    while (EOF != (ch = fgetc(file)))
    {
        if (ch == '=')
        {
            int flat = 1;
            double num = 0.1;
            char ch_t = 0;
            while ('s' != (ch_t = fgetc(file)))
            {
                
                if (ch_t == '.')
                {
                    flat = 0;
                    continue;
                }
                //printf("%c\n",ch_t);
                if (flat)
                {
                    result *= 10;
                    result += (ch_t - '0');
                }
                else
                {
                    result = result + (ch_t - '0') * num;
                    num *= 0.1;
                    //printf("%lf\t%lf\n",point,result);
                }
                
                
            }
            break;
        }
    }
    fclose(file);
    return result;
}````


本文链接http://www.taodudu.cc/news/show-1944823.html