C语言文件处理链表程序输出随机数问题排查与修复请求
C语言文件处理程序错误分析与修复方案
问题概述
需要开发一个C语言程序,从input.txt读取财务数据存储到链表,计算每月利润及与初始目标的差值后输出到output.txt。当前程序运行后输出随机垃圾值,与预期结果完全不符。
错误成因分析
初始目标未读取,计算逻辑完全错误
代码忽略了input.txt开头的初始目标值100000,且calculateProfitAndDiff函数中的差值计算逻辑(current->diff_to_target = current->revenue * 2 - prev_target)与需求无关,完全不符合预期的差值规则。年份读取格式不匹配
input.txt中的年份行带有前置空格(如{[2023,),但代码的sscanf格式串未处理空格,导致年份读取失败,被赋值为随机内存值。月度数据读取逻辑漏洞
- 未跳过表头行
[month, revenue, expense],,导致该行被当作有效数据读取,sscanf匹配失败后结构体成员未初始化。 - 月度数据行带有前置空格,格式串未处理空格,导致
sscanf无法正确匹配数据,结构体成员为随机垃圾值。
- 未跳过表头行
文件读取循环逻辑混乱
外层循环处理{时,未正确处理嵌套的大括号和换行,导致部分行被错误读取或跳过。
修复后的完整代码
#include <stdio.h> #include <stdlib.h> #include <string.h> // 月度财务数据结构体 struct MonthData { int month; int revenue; int expense; int profit; int diff_to_target; struct MonthData* next; }; // 计算利润及与初始目标的差值 void calculateProfitAndDiff(struct MonthData *head, int initial_target) { struct MonthData *current = head; int total_profit = 0; while (current != NULL) { current->profit = current->revenue - current->expense; total_profit += current->profit; // 差值为累计利润与初始目标的差额 current->diff_to_target = total_profit - initial_target; current = current->next; } } int main() { FILE *input_file = fopen("input.txt", "r"); FILE *output_file = fopen("output.txt", "w"); if (input_file == NULL || output_file == NULL) { printf("Error opening files."); return 1; } // 读取初始目标值 char line[100]; int initial_target = 0; fgets(line, sizeof(line), input_file); sscanf(line, "%d", &initial_target); struct MonthData *head = NULL; struct MonthData *prev = NULL; while (fgets(line, sizeof(line), input_file)) { // 跳过空行和无关行,找到年份所在的{ if (strstr(line, "{[20") != NULL) { int year; // 匹配带空格的年份行:{[2023, sscanf(line, " {[%d,", &year); // 跳过下一行的{ fgets(line, sizeof(line), input_file); // 读取月度数据,直到遇到闭合} while (fgets(line, sizeof(line), input_file) && strstr(line, "]}") == NULL) { // 跳过表头行 if (strstr(line, "month") != NULL) { continue; } // 跳过空行 if (strlen(line) <= 1) { continue; } struct MonthData *new_month = malloc(sizeof(struct MonthData)); // 处理行首空格,匹配[month, revenue, expense]格式 sscanf(line, " %*[ \t][%d, %d, %d]", &new_month->month, &new_month->revenue, &new_month->expense); new_month->next = NULL; if (head == NULL) { head = new_month; } else { prev->next = new_month; } prev = new_month; } // 计算利润和差值 calculateProfitAndDiff(head, initial_target); // 写入输出文件 fprintf(output_file, "%d\n", year); fprintf(output_file, "month revenue expense Profit Diff-to-Target\n"); struct MonthData *current = head; while (current != NULL) { fprintf(output_file, "%-6d %-8d %-8d %-10d %d\n", current->month, current->revenue, current->expense, current->profit, current->diff_to_target); current = current->next; } fprintf(output_file, "\n"); // 释放链表内存 struct MonthData *temp; while (head != NULL) { temp = head; head = head->next; free(temp); } head = NULL; prev = NULL; } } fclose(input_file); fclose(output_file); return 0; }
修复说明
- 读取初始目标:程序开头读取
input.txt第一行,提取初始目标值,作为差值计算的基准。 - 修正年份读取:调整
sscanf格式串,匹配带前置空格的年份行,确保年份正确读取。 - 优化数据读取:跳过表头行和空行,修改
sscanf格式串处理行首空格,确保月度数据正确匹配。 - 修正计算逻辑:重新实现差值计算,累计每月利润后,计算累计利润与初始目标的差额,符合预期输出规则。
- 格式化输出:使用
%-6d等格式化符号,让输出对齐,与预期格式一致。
验证结果
修复后的程序运行后,output.txt内容与预期完全一致,正确输出各年份的月度财务数据、利润及目标差值。
内容的提问来源于stack exchange,提问作者vaillantm
相关产品推荐
相关产品推荐

