C语言实现TXT单词存入数组遇阻,现有代码存在编译警告求解决
Hey there! Let's break down what's going wrong with your code and fix it step by step:
1. 先搞定编译警告
第一个警告:格式参数不匹配
filetest.c:22:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘FILE * {aka struct __sFILE64 *}’ [-Wformat=]
printf("%s", fpointer);
You accidentally passed the FILE* pointer fpointer to printf's %s specifier — %s needs a string pointer (char*). You meant to print the content you just read, so change this line to printf("%s", singleline);.
第二个警告:参数类型不兼容
filetest.c:23:11: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
strcpy(list[i], singleline);
list is a one-dimensional char array, so list[i] is a single character (char type). But strcpy requires a char* (string pointer) as its first argument. To store multiple words, you need either a 2D char array or an array of pointers to hold each word's address.
2. Fix other logic issues in your code
- The
while (!feof(fpointer))pattern is problematic:feofonly returns true after you've tried to read past the end of the file, which can cause an extra, invalid read. Instead, use the return value offgetsto control the loop. - The inner
for (i = 0; i < MAX; i++)is unnecessary — you don't need to loop 150 times; you just need to read until the file ends or you've collected all words. - Your input file has space-separated words on one line, so
fgetswill read the entire line first. You need to split this line into individual words instead of storing the whole line directly.
3. Corrected Code
Here's a working version of your program with key improvements explained in comments:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_WORDS 100 // Max number of words we can store #define MAX_WORD_LEN 50 // Max length of each individual word int main() { FILE *fpointer = fopen("input.txt", "r"); // Always check if the file opened successfully if (fpointer == NULL) { perror("Failed to open file"); return 1; } char singleline[MAX_WORD_LEN * MAX_WORDS]; // Store the entire line from the file char word_array[MAX_WORDS][MAX_WORD_LEN]; // 2D array: each row holds one word int word_count = 0; // Read the entire line (assuming all words are on one line) if (fgets(singleline, sizeof(singleline), fpointer) != NULL) { // Split the line into words using spaces/newlines/tabs as delimiters char *token = strtok(singleline, " \n\t"); while (token != NULL && word_count < MAX_WORDS) { // Copy the split word into our array strcpy(word_array[word_count], token); word_count++; // Get the next word token = strtok(NULL, " \n\t"); } } // Test output to verify the words are stored correctly printf("Successfully loaded %d words:\n", word_count); for (int i = 0; i < word_count; i++) { printf("array[%d] = %s\n", i, word_array[i]); } fclose(fpointer); return 0; }
4. Key Improvements Explained
- Added file open error checking: Prevents crashes if the file doesn't exist or you don't have permission to read it.
- Used a 2D array
word_arrayto store each word individually, with dedicated space for every entry. - Used
strtokto split the input line into space-separated words, matching your input file's format. - Tracked the actual number of words with
word_countto avoid array overflow. - Replaced the faulty
while (!feof)pattern with a check onfgets' return value, ensuring we only process valid content.
This code will store each word from your input.txt into array[0], array[1], etc., ready for your后续 analysis.
内容的提问来源于stack exchange,提问作者Michael Bacha

