程序编译无错误但运行至while循环时触发Segfault,请求排查
Alright, let's break down why your program is hitting a segfault when it reaches that while loop—this is a super common pain point with CSV parsing and struct handling, so let's walk through the most likely culprits and fixes:
Common Causes & Fixes
Uninitialized pointers/struct members
If your struct useschar*fields instead of fixed-size char arrays, you need to explicitly allocate memory withmalloc()before copying data into them. For example, if your struct haschar* username;, skippingusername = malloc(256);(or a size matching your data) and callingstrcpy(username, csv_field)will write to unallocated memory, triggering a segfault. Also double-check that you're not using NULL pointers anywhere in your struct initialization.Invalid file pointer
Did you verify thatfopen()actually succeeded? If your CSV file doesn't exist, you don't have read permissions, or you mistyped the file path,fopen()returns a NULL pointer. Trying to runfgets()on a NULL file pointer will immediately crash your program. Add a check right after opening the file:FILE* fp = fopen("data.csv", "r"); if (fp == NULL) { perror("Failed to open CSV file"); exit(EXIT_FAILURE); }Buffer issues with
fgets()
If your buffer size is too small for long CSV lines,fgets()will truncate the line, leaving partial data that can break your parsing logic (and lead to out-of-bounds memory access). Make sure your buffer is large enough (e.g.,#define BUFFER_SIZE 1024or bigger depending on your data). Also, never use an uninitialized buffer withfgets()—always declare it as a fixed-size array on the stack or allocate it withmalloc().Broken CSV parsing logic
When usingstrtok()(or another parsing method) to split CSV lines, if a line has fewer fields than your struct expects, you might end up trying to access a NULL pointer returned bystrtok(). For example, if your struct needs 3 fields but a CSV line only has 2, the thirdstrtok()call returns NULL, and using that instrcpy()will cause a segfault. Add checks to ensure each expected field exists before assigning it to your struct.Incorrect while loop condition
A common mistake is writingwhile (fgets(buffer, SIZE, fp) != EOF)—butfgets()returns a pointer to the buffer on success, and NULL on EOF or error. The correct condition iswhile (fgets(buffer, SIZE, fp) != NULL). Using the wrong condition can lead to processing invalid data in the loop.
Debugging Tips
- Use
gdbto pinpoint the crash: Compile your program with the-gflag, then rungdb ./your_program. When it segfaults, typebtto get a backtrace that shows exactly which line caused the crash. - Add debug prints: Print each line you read with
fgets(), and print the values of each struct field after parsing. This will help you spot when data becomes invalid (like NULL pointers or garbage values). - Validate input: Add checks for empty lines, malformed CSV entries, and ensure all struct fields are properly populated before passing them to your output function.
内容的提问来源于stack exchange,提问作者HackMaster

