C语言:将归档文件每行存入数组后输出为0的问题求助
Let's walk through what's probably causing your output to show all zeros instead of the expected line content, and how to fix it:
Common Issues & Fixes
1. You're Not Properly Processing the Comma-Separated Input
Your input uses commas to split entries (1760 02/20/18,11403.7 02/19/18,...), so if you're trying to store each segment in a struct, you need explicit string-splitting logic. Skipping this step means your struct array stays unpopulated (or filled with garbage/zeros).
2. Unsafe String Copying (Missing Null Terminator)
If you're using strcpy without length checks, or forgetting to add a null terminator, your struct's data array might end up with invalid data. strncpy is safer here, but it doesn't automatically add a null terminator—you have to do that manually to avoid weird output or crashes.
3. Wrong Printf Format Specifier
Your output shows 1761 0 instead of the expected string—this is a clear sign you're using a numeric format specifier (like %d) instead of %s to print the data field. Printing a string with %d outputs the memory address as an integer, which often looks like zero.
4. Missing File Error Checking
If your code doesn't verify the file opened successfully, you might be reading from a NULL file pointer. That means no data gets loaded into your array at all, leaving it filled with default zeros.
Corrected Code Example
Here's a fixed version of your code that addresses all these problems:
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct{ char data[20]; } vetor; int main(int argc, char *argv[]) { // Make sure a file path is provided as an argument if (argc < 2) { fprintf(stderr, "Usage: %s <archive_file_path>\n", argv[0]); exit(EXIT_FAILURE); } // Open the file with proper error handling FILE *fp = fopen(argv[1], "r"); if (fp == NULL) { perror("Failed to open file"); exit(EXIT_FAILURE); } vetor v[100]; // Adjust array size to match your needs int entry_count = 0; char line_buffer[1024]; // Read the input line (assuming your archive has one line of comma-separated entries) if (fgets(line_buffer, sizeof(line_buffer), fp) != NULL) { // Split the line into individual entries using commas char *token = strtok(line_buffer, ","); while (token != NULL && entry_count < 100) { // Remove any trailing newline from the token token[strcspn(token, "\n")] = '\0'; // Safely copy the token to the struct's data field strncpy(v[entry_count].data, token, sizeof(v[entry_count].data) - 1); // Manually add null terminator to avoid buffer overflow issues v[entry_count].data[sizeof(v[entry_count].data) - 1] = '\0'; entry_count++; token = strtok(NULL, ","); } } fclose(fp); // Print the results with the correct format specifier for (int i = 0; i < entry_count; i++) { printf("%d %s\n", i + 1, v[i].data); } return 0; }
Key Changes Explained
- Added input validation to ensure a file path is provided, plus error checking for file opening
- Used
strtokto split the comma-separated input into individual entries for the struct array - Implemented safe string copying with
strncpyand manual null termination to prevent buffer overflow - Fixed the
printfstatement to use%s(the correct specifier for strings) - Added logic to strip trailing newlines from each entry for clean output
内容的提问来源于stack exchange,提问作者Andre

