You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言WhatsApp聊天分析器:成员姓名存储数组填充异常问题求助

Hey there! Let's dig into that end_array issue with your WhatsApp chat analyzer—string parsing and deduplication can be tricky, but I’ve worked through similar problems before, so let’s break this down.

First, let’s recap your core goal: extract the username between - and : from each chat line, skip duplicates, and store unique names in end_array. The most likely culprits for your wrong fill issue are flawed string parsing logic, incorrect duplicate checking, or array boundary mismanagement. Here’s how to fix it step by step:

1. Fix the Username Extraction Logic

The biggest mistake often comes from not targeting the exact separators (- and : , including spaces). If you’re just searching for single - or : characters, you’ll accidentally pick up date separators or other symbols. Instead, use strstr() to locate the exact delimiter pairs:

  • First, find the position of - (space + hyphen + space) to skip past the timestamp.
  • Then, from that point, find the next : (colon + space) to mark the end of the username.
  • Extract the substring between these two points, making sure to add a null terminator to avoid garbage characters.

2. Correct Duplicate Checking

Never use == to compare strings in C—this checks pointer addresses, not actual content. Use strcmp() instead, which returns 0 only when two strings are identical. Also, track how many unique users you’ve already stored to avoid writing past the end of end_array.

3. Example Working Code Snippet

Here’s a revised version of your core logic that addresses these issues:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_UNIQUE_USERS 150  // Adjust based on your chat size
#define MAX_NAME_LENGTH 60

int main() {
    FILE *chat_file = fopen("whatsapp_chat.txt", "r");
    if (!chat_file) {
        perror("Failed to open chat file");
        return 1;
    }

    char end_array[MAX_UNIQUE_USERS][MAX_NAME_LENGTH] = {0};
    int user_count = 0;  // Tracks how many unique users we've stored
    char line_buffer[256];  // Holds each line from the chat file

    while (fgets(line_buffer, sizeof(line_buffer), chat_file)) {
        // Locate the exact separator after the timestamp: " - "
        char *dash_sep = strstr(line_buffer, " - ");
        if (!dash_sep) continue;  // Skip lines without valid user prefix (e.g., system messages)

        // Move pointer past " - " to the start of the username
        char *name_start = dash_sep + 3;

        // Locate the end of the username: ": "
        char *colon_sep = strstr(name_start, ": ");
        if (!colon_sep) continue;  // Skip malformed lines

        // Calculate username length and validate it's within bounds
        int name_length = colon_sep - name_start;
        if (name_length <= 0 || name_length >= MAX_NAME_LENGTH) continue;

        // Store username in a temporary buffer
        char temp_name[MAX_NAME_LENGTH];
        strncpy(temp_name, name_start, name_length);
        temp_name[name_length] = '\0';  // Critical: add null terminator

        // Check if username already exists in end_array
        int is_duplicate = 0;
        for (int i = 0; i < user_count; i++) {
            if (strcmp(end_array[i], temp_name) == 0) {
                is_duplicate = 1;
                break;
            }
        }

        // If not a duplicate and we have space, add to end_array
        if (!is_duplicate && user_count < MAX_UNIQUE_USERS) {
            strcpy(end_array[user_count], temp_name);
            user_count++;
        }
    }

    // Test: print all unique users
    printf("Unique chat members:\n");
    for (int i = 0; i < user_count; i++) {
        printf("- %s\n", end_array[i]);
    }

    fclose(chat_file);
    return 0;
}

Key Notes to Avoid Future Issues

  • Skip invalid lines: System messages (like "You joined the group") won’t have the - separator, so we skip them with continue to avoid garbage data.
  • Boundary checks: We validate username length and user_count to prevent writing past the end of end_array (a common cause of weird "wrong fill" behavior).
  • Null terminators: Always manually add \0 when using strncpy()—it doesn’t add one automatically, which leads to corrupted strings.

内容的提问来源于stack exchange,提问作者ProjectX

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:26:13