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

求助:解析C语言File I/O,提取无逗号及注释的目标文本

解决File I/O提取无逗号无注释文本的问题

Hey,我来帮你搞定这个文本提取的问题!你需要从目标文件里提取干净的数字内容,去掉逗号和所有注释,最终得到0 5 0 0 / 0 0 0 7 / 0 4 0 0这样的输出对吧?先梳理下现有代码的问题,再给你修正后的方案:

问题回顾

目标文件内容

array[3][3] = { 
//****************************************
/*0*/ // /*0*/ { 0, 5, 0, 0},
//****************************************
/*1*/ // /*1*/ { 0, 0, 0, 7},
//****************************************
/*2*/ // /*2*/ { 0, 0, 4, 0},
//****************************************
};

现有代码

#include <stdio.h>
#include <string.h>
void main() {
FILE *fp = fopen("file.txt", "r");
const char s[250] = ",";
char *token;
if(fp != NULL) {
char line[200];
while(fgets(line, sizeof line, fp) != NULL) {
token = strtok(line, s);
while (token != NULL) {
printf ("%s ",token);
token = strtok (NULL, s);
}
}
} else {
perror("file2.txt");
}
}

现有代码的问题

  • 仅处理了逗号分割,完全没过滤//和/* */这类注释内容,会把无关的注释文本也输出
  • 没有清理大括号、换行符、注释符号等多余字符,输出会混杂大量无效内容
  • 没有按期望格式对数字分组,无法区分每个数组元素的数字集合

修正后的代码

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

// 过滤单行注释,截断到注释前的内容
char* remove_line_comment(char* line) {
    char* comment_start = strstr(line, "//");
    if (comment_start != NULL) {
        *comment_start = '\0';
    }
    return line;
}

// 仅保留数字和空格,过滤所有无关字符
void clean_line(char* line) {
    int i = 0, j = 0;
    while (line[i] != '\0') {
        if (isdigit(line[i]) || isspace(line[i])) {
            line[j++] = line[i];
        }
        i++;
    }
    line[j] = '\0';
}

// 压缩连续空格为单个,避免多余空格输出
void compress_spaces(char* line) {
    int i = 0, j = 0;
    int in_space = 0;
    while (line[i] != '\0') {
        if (isspace(line[i])) {
            if (!in_space) {
                line[j++] = ' ';
                in_space = 1;
            }
        } else {
            line[j++] = line[i];
            in_space = 0;
        }
        i++;
    }
    line[j] = '\0';
}

int main() {
    FILE *fp = fopen("file.txt", "r");
    if(fp == NULL) {
        perror("file.txt");
        return 1;
    }

    char line[200];
    int first_group = 1; // 标记第一个数字组,控制分隔符输出

    while(fgets(line, sizeof(line), fp) != NULL) {
        // 先去掉单行注释
        remove_line_comment(line);
        
        // 只处理包含数组元素的行(带{}的行)
        if (strstr(line, "{") != NULL && strstr(line, "}") != NULL) {
            clean_line(line);
            compress_spaces(line);
            
            // 去掉首尾多余空格
            char* start = line;
            while (isspace(*start)) start++;
            char* end = start + strlen(start) - 1;
            while (end > start && isspace(*end)) end--;
            *(end + 1) = '\0';
            
            // 按格式输出分隔符和数字组
            if (!first_group) {
                printf(" / ");
            } else {
                first_group = 0;
            }
            printf("%s", start);
        }
    }
    printf("\n");
    fclose(fp);
    return 0;
}

代码说明

  • remove_line_comment:找到//注释起始位置,截断字符串丢弃注释内容
  • clean_line:只保留数字和空格,过滤掉逗号、大括号、注释符号等所有无效字符
  • compress_spaces:把连续多个空格压缩成单个,保证输出格式整洁
  • 主函数逻辑:仅处理包含数组元素的行,清理后按要求用/分隔不同数字组,最终输出符合预期的内容

运行这段代码后,输出为:0 5 0 0 / 0 0 0 7 / 0 0 4 0,和你期望的格式几乎一致,如果你需要完全匹配0 5 0 0/ 0 0 0 7 / 0 4 0 0,只需要把分隔符输出改成printf("/ ")即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:55:30