C语言定位并抓取文件中的特定字符串
发布日期:2021-05-14 10:08:35 浏览次数:16 分类:精选文章

本文共 2191 字,大约阅读时间需要 7 分钟。

������������������������������������������������������������C���������������������������������������������������TXT������������������������������

������������������������������������������������������" is"������������������������������������������������������������������������������������������������������������������" is" ���������������������������������������������������������������������������������������������������������������������������������������������

���������������������������������

  • ������ fget() ���������������������������������������
  • ������ strstr() ������������" is"������������
  • ������������������������������������" is" ���������������������������������������������������������������
  • ������������������������������������������������������
  • ���������������������������������������������������������
  • ���������������������������������

    #include 
    #include
    #include
    int main() {
    FILE *input_file;
    unsigned int file_size = 0;
    char line[64];
    char result[300][64] = {0};
    int result_final[300] = {0};
    int i = 0;
    int len = 0;
    int len1 = 0;
    int a = 0;
    int b = 1;
    input_file = fopen("test.txt", "rb+");
    if (input_file == NULL) {
    printf("can not find file!\n");
    return 1;
    } else {
    printf("File opened successful!\n");
    }
    while (fgets(line, sizeof(line), input_file)) {
    char *pLast = strstr(line, " is");
    if (pLast != NULL) {
    pLast += 3;
    while (*pLast != ' ') {
    len++;
    pLast++;
    }
    if (!pLast) {
    printf("No number found!\n");
    continue;
    }
    memcpy(result[i], pLast - len, len);
    i++;
    }
    }
    fclose(input_file);
    for (int j = 0; j < 300; ++j) {
    if (result_final[j] == 0 && result[j][0] != '\0') {
    result_final[j] = strtoll(result[j], nullptr, 10);
    }
    }
    for (int j = 0; j < 300; ++j++) {
    printf("%d\n", result_final[j]);
    }
    return 0;
    }

    ���������������������������������������������������������������������������������������������������������

    上一篇:嵌入式中缓冲区队列的实现
    下一篇:内存操作——memcpy函数用法

    发表评论

    最新留言

    路过按个爪印,很不错,赞一个!
    [***.219.124.196]2025年04月17日 07时24分37秒