Mac平台C代码编译运行出现Segmentation fault:11问题求助
Yep, this is a super common pitfall when working with file I/O in C. Let's break down exactly what's causing that Segmentation fault: 11 and how to fix it.
The Root Causes
1. Missing Error Checks for File Openings
The biggest culprit here is that you're not verifying if fopen succeeded. If:
- The
b.datafile doesn't exist in your current working directory - You don't have read permissions for
b.data - You can't create/write to
found.data(e.g., permission denied)
fopen will return a NULL pointer. Trying to use fscanf or fprintf on a NULL file pointer is undefined behavior—and that's almost certainly why you're seeing the segmentation fault.
2. No Validation for Input Data
If your b.data file doesn't contain exactly N+1 integers (one target value followed by 300 elements), your loop will keep trying to read data even when there's none left. This can lead to reading invalid memory, which might also trigger a seg fault or other unpredictable behavior.
Fixed Code with Proper Error Handling
Here's your code updated with all the necessary checks to prevent the seg fault and help you debug issues:
#include "stdio.h" const int N=300; int main(){ FILE * infile, * outfile; int i, target, b[N]; // Open input file with error check infile=fopen("b.data","r"); if (infile == NULL) { perror("Failed to open b.data"); // Prints why the open failed return 1; // Exit with non-zero error code } // Open output file with error check outfile=fopen("found.data","w"); if (outfile == NULL) { perror("Failed to open found.data"); fclose(infile); // Clean up the input file we did open return 1; } // Read target with validation if (fscanf(infile, "%d", &target) != 1) { fprintf(stderr, "Error reading target value from b.data\n"); fclose(infile); fclose(outfile); return 1; } // Read array elements with validation for (i=0; i<N;i++){ if (fscanf(infile, "%d", &b[i]) != 1) { fprintf(stderr, "Error reading element %d from b.data\n", i+1); fclose(infile); fclose(outfile); return 1; } } // Write matches to output file for(i=0;i<N;i++){ if (b[i]==target){ fprintf(outfile, "%d\n", i+1); } } // Always close files to release resources and flush data fclose(infile); fclose(outfile); return 0; }
Key Improvements Explained
- File open checks: Using
perrorwill tell you exactly why the file couldn't be opened (e.g., "No such file or directory" ifb.datais missing). - Input validation: Checking the return value of
fscanfensures we're actually reading the data we expect.fscanfreturns the number of items successfully read—so we check if it equals 1 for each integer. - Resource cleanup: We close files before exiting on error to avoid leaving open file handles, and we always close them at the end to make sure output data is flushed to
found.data.
The most likely immediate fix for your seg fault is making sure b.data exists in the directory where you're running the program. Adding the error checks will confirm this and help you troubleshoot any other issues.
内容的提问来源于stack exchange,提问作者MatlabNewb

