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

Dev-C++下读取CSV计算温度均值的C代码运行失败,如何修复?

问题原因
  • 未跳过CSV表头行:你的CSV第一行是Test File #1 35 lines inc. header,不符合数字,数字,数字,数字的格式,首次fscanf直接匹配失败,lineCount保持为0,后续计算平均值时触发除0错误,同时sumTemp1等求和变量未初始化,读取垃圾值触发运行异常。
  • localtime参数类型不匹配:你定义了time_t seconds = date;但调用localtime时却传了&date(unsigned long类型指针),Windows下time_t默认是64位整数,类型不匹配会导致localtime返回空指针,后续访问tm->tm_mday等成员时触发空指针访问崩溃。
  • 求和变量未初始化:sumTemp1/sumTemp2/sumTemp3定义时未赋值为0,若读取数据失败直接参与计算会得到不可预期的结果。
  • ANSI颜色转义码兼容问题:Dev-C++默认运行的Windows控制台未开启虚拟终端支持,你定义的颜色宏会输出乱码,也可能引发输出异常。
修复方案

核心修改点

  1. 打开文件后先用fgets读取并丢弃第一行表头
  2. 修正localtime入参为&seconds,增加空指针判断避免崩溃
  3. 初始化所有求和变量为0.0,计算平均值前判断lineCount不为0,避免除0错误
  4. 暂时移除颜色转义码保证输出正常,需要颜色的话可额外添加Windows控制台虚拟终端开启代码

修复后完整代码

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

// 打印可读格式日期
void printDate(unsigned long date) {
    time_t seconds = date;
    struct tm* tm = localtime(&seconds); // 修正入参为正确的time_t类型指针
    if (tm == NULL) { // 增加空指针判断
        printf("无效时间戳");
        return;
    }
    char months[][4] = {
        "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
        "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
    };
    printf("%02d-%s-%d %02d:%02d:%02d", tm->tm_mday, months[tm->tm_mon], tm->tm_year + 1900,
        tm->tm_hour, tm->tm_min, tm->tm_sec);
}

// 读数转换为华氏度
double readingToFahrenheit(double reading) {
    double mv = ((reading / 2047) * 5000);
    double kelvin = mv / 10;
    return (kelvin - 273.15) * (9.0/5.0) + 32;
}

// 打印温度输出
void printOutput(double temp) {
    printf("%.1fºF", temp);
}

int main(void) {
    char filePath[256];
    printf("Enter The File Path: ");
    fgets(filePath, sizeof(filePath) - 1, stdin);
    filePath[strcspn(filePath, "\n")] = '\0'; // 更安全的换行移除方式,避免空字符串问题

    FILE * fp = fopen(filePath, "r");
    if (fp == NULL) {
        printf("Error: %s Cannot Be Opened!\n", filePath);
        return -1;
    }

    // 跳过表头行
    char headerBuf[256];
    fgets(headerBuf, sizeof(headerBuf), fp);

    unsigned long date, startDate;
    int temp1, temp2, temp3;
    int lineCount = 0;
    double sumTemp1 = 0.0, sumTemp2 = 0.0, sumTemp3 = 0.0; // 初始化求和变量

    // 读取第一行数据记录起始时间
    if ((fscanf(fp, "%lu,%d,%d,%d", &date, &temp1, &temp2, &temp3)) == 4) {
        startDate = date;
        sumTemp1 = temp1;
        sumTemp2 = temp2;
        sumTemp3 = temp3;
        lineCount++;
    }

    // 读取剩余所有行
    while ((fscanf(fp, "%lu,%d,%d,%d", &date, &temp1, &temp2, &temp3)) == 4) {
        sumTemp1 += temp1;
        sumTemp2 += temp2;
        sumTemp3 += temp3;
        lineCount++;
    }
    fclose(fp); // 关闭文件释放资源

    if (lineCount == 0) {
        printf("未读取到有效数据\n");
        return -1;
    }

    // 输出结果
    printf("Starting time ");
    printDate(startDate);
    printf("\n\nEnding time ");
    printDate(date);

    // 计算平均值
    double temp1Avg = sumTemp1 / lineCount;
    double temp2Avg = sumTemp2 / lineCount;
    double temp3Avg = sumTemp3 / lineCount;

    printf("\nTemperature Sensor 1 Average:  ");
    printOutput(readingToFahrenheit(temp1Avg));
    printf("\nTemperature Sensor 2 Average: ");
    printOutput(readingToFahrenheit(temp2Avg));
    printf("\nTemperature Sensor 3 Average:  ");
    printOutput(readingToFahrenheit(temp3Avg));
    printf("\n");

    return 0;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 15:45:07