C语言人力资源管理控制台程序段错误求助:文件检查与读取问题
Hey there, let's break down the issues in your code and get that HR app running smoothly!
1. Wrong File Mode for Existence Check
Your current code uses "w" mode with fopen():
FILE *userListFile=fopen("userlist.txt", "w");
This is a critical mistake—"w" mode creates the file if it doesn't exist, or truncates (empties) it if it does. Not only does this make your existence check useless, but it'll wipe out any existing employee data!
To properly check if the file exists, use "r" mode instead. If the file doesn't exist, fopen() will return NULL:
FILE *userListFile = fopen("userlist.txt", "r"); if (userListFile == NULL) { // File doesn't exist userListExist = false; } else { // File exists, proceed to read userListExist = true; // ... add your read logic here fclose(userListFile); // Don't forget to close the file! }
2. Potential Stack Overflow Causing Segmentation Fault
You're declaring a large array directly on the stack:
struct user_details userlist[number_of_employee];
The stack has limited memory (usually just a few megabytes). If number_of_employee is a large value (like 1000 or more), this will overflow the stack and trigger a segmentation fault.
Instead, allocate memory on the heap using malloc() (and remember to free it later to avoid memory leaks):
struct user_details *userlist = malloc(number_of_employee * sizeof(struct user_details)); if (userlist == NULL) { // Handle memory allocation failure perror("Failed to allocate memory"); return false; } // ... use the userlist array for reading data ... free(userlist); // Clean up when you're done with the array
3. Other Quick Crash Prevention Checks
- Double-check that
number_of_employeeis properly defined (not an uninitialized variable or an unreasonably huge value). - Always validate the return values of
fopen()andmalloc()—ignoring errors is one of the most common causes of unexpected crashes. - If you're reading entries into the
userlistarray, make sure you don't read more entries thannumber_of_employee(out-of-bounds array access will also trigger seg faults).
Here's a revised version of your function putting all these fixes together:
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> // Example struct definition (adjust to match your actual fields) struct user_details { int emp_id; char emp_name[50]; float emp_salary; }; #define number_of_employee 100 // Set to your actual expected employee count bool userListAvailable() { bool userListExist = false; struct user_details *userlist = malloc(number_of_employee * sizeof(struct user_details)); if (userlist == NULL) { perror("Memory allocation failed"); return false; } FILE *userListFile = fopen("userlist.txt", "r"); if (userListFile != NULL) { userListExist = true; // Example read logic (tweak based on your file's format) int i = 0; while (i < number_of_employee && fscanf(userListFile, "%d %s %f", &userlist[i].emp_id, userlist[i].emp_name, &userlist[i].emp_salary) == 3) { i++; } fclose(userListFile); } free(userlist); return userListExist; }
内容的提问来源于stack exchange,提问作者David NIWEWE

