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

如何用C语言以表格列格式输出CSV文件内容?

如何将CSV内容以对齐的表格形式输出(C语言)

问题说明

作为C语言新手,需要将CSV文件内容按列对齐的表格形式输出。当前输出因字段(如名称)长度差异大,阅读困难,希望实现规整的列布局。

CSV示例内容

code; name; state; owner; latitude; longitude; kind; size; difficulty; terrain; status; hidden_date; founds; not_founds; favourites; altitude
GCQC0V;Lenda Do Galo de Barcelos;BRAGA;ar-my;41.5282;-8.622067;MULTI;SMALL;2;1.5;AVAILABLE;2005/08/29;361;21;4;17
GCZP2A;Chromite - FeOCr2O3 - DP/EC5;BRAGANCA;danieloliveira;41.802433;-6.88395;EARTHCACHE;NOT_CHOSEN;1;1.5;AVAILABLE;2006/12/05;62;2;2;838
GCMAM8;A Lagoa [Obidos];LEIRIA;paulohercules;39.390417;-9.225283;TRADITIONAL;REGULAR;2;2;AVAILABLE;2004/12/22;703;9;3;19

现有代码问题

当前代码使用普通printf空格分隔输出,没有固定字段宽度,导致列无法对齐。同时sscanf未处理CSV中分号后的空格,会把空格读入字符串字段,影响输出效果。

现有主代码

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

int main() {
    Cache cache;
    FILE *fp;
    fp = fopen("caches_all.csv", "r");

    char myString[1000];
    fgets(myString, 1000, fp);

    if (fp != NULL) {
        while (fgets(myString, 1000, fp)) {
            sscanf(myString, "%[^;];%[^;];%[^;];%[^;];%lf;%lf;%[^;];%[^;];%lf;%lf;%[^;];%[^;];%d;%d;%d;%lf",
                cache.code, cache.name, cache.state, cache.owner, &cache.latitude, &cache.longitude,
                cache.kind, cache.size, &cache.difficulty, &cache.terrain, cache.status,
                cache.hidden_date, &cache.founds, &cache.not_founds, &cache.favorites, &cache.altitude);

            printf("%s %s %s %s %.6lf %.6lf %s %s %.1lf %.1lf %s %s %d %d %d %.0lf\n",
                cache.code, cache.name, cache.state, cache.owner, cache.latitude, cache.longitude,
                cache.kind, cache.size, cache.difficulty, cache.terrain, cache.status,
                cache.hidden_date, cache.founds, cache.not_founds, cache.favorites, cache.altitude);
        }
    } else {
        printf("Not able to open the file.");
        return 1;
    }

    fclose(fp);

    return 0;
}

头文件(input.h)

#ifndef INPUT_H
#define INPUT_H

typedef struct cache {
    char code[500];
    char name[500];
    char state[500];
    char owner[500];
    double latitude;
    double longitude;
    char kind[500];
    char size[500];
    double difficulty;
    double terrain;
    char status[500];
    char hidden_date[500];
    int founds;
    int not_founds;
    int favorites;
    double altitude;
} Cache;

#endif

解决方案

通过printf格式化宽度控制实现列对齐,同时修正sscanf的格式符以处理CSV中分号后的空格。

修改后的代码

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

int main() {
    Cache cache;
    FILE *fp;
    fp = fopen("caches_all.csv", "r");

    char myString[1000];
    // 读取表头并跳过
    if (fgets(myString, 1000, fp) == NULL) {
        printf("File is empty or read error.");
        return 1;
    }

    if (fp != NULL) {
        while (fgets(myString, 1000, fp)) {
            // 修正sscanf格式,处理分号后的空格
            sscanf(myString, "%[^;]; %[^;]; %[^;]; %[^;];%lf;%lf;%[^;];%[^;];%lf;%lf;%[^;];%[^;];%d;%d;%d;%lf",
                cache.code, cache.name, cache.state, cache.owner, &cache.latitude, &cache.longitude,
                cache.kind, cache.size, &cache.difficulty, &cache.terrain, cache.status,
                cache.hidden_date, &cache.founds, &cache.not_founds, &cache.favorites, &cache.altitude);

            // 使用带宽度的格式化输出,左对齐保证列对齐
            printf("%-10s %-35s %-10s %-20s %-12.6lf %-12.6lf %-15s %-12s %-8.1lf %-8.1lf %-10s %-12s %-6d %-10d %-8d %.0lf\n",
                cache.code, cache.name, cache.state, cache.owner, cache.latitude, cache.longitude,
                cache.kind, cache.size, cache.difficulty, cache.terrain, cache.status,
                cache.hidden_date, cache.founds, cache.not_founds, cache.favorites, cache.altitude);
        }
    } else {
        printf("Not able to open the file.");
        return 1;
    }

    fclose(fp);

    return 0;
}

关键修改说明

  1. 修正sscanf格式:将%[^;];%[^;]改为%[^;]; %[^;],跳过分号后的空格,避免字符串字段前出现多余空格。
  2. printf格式化宽度:
    • %-10s:左对齐,占10个字符宽度,适用于短字符串(如code、state)
    • %-35s:预留足够宽度给最长的name字段,避免内容截断
    • %-12.6lf:浮点数占12位宽度,保留6位小数,适配经纬度
    • 数字类型(如founds)设置固定宽度,保证列对齐

对齐后的输出示例

GCQC0V     Lenda Do Galo de Barcelos       BRAGA      ar-my                41.528200    -8.622067    MULTI           SMALL        2.0      1.5      AVAILABLE   2005/08/29   361    21          4        17
GCZP2A     Chromite - FeOCr2O3 - DP/EC5    BRAGANCA   danieloliveira       41.802433    -6.883950    EARTHCACHE      NOT_CHOSEN   1.0      1.5      AVAILABLE   2006/12/05   62     2           2        838
GCMAM8     A Lagoa [Obidos]                LEIRIA     paulohercules        39.390417    -9.225283    TRADITIONAL     REGULAR      2.0      2.0      AVAILABLE   2004/12/22   703    9           3        19

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 21:07:34