C语言求助:.txt文件单词拆分、含指定字符单词提取及代码排查
How to Split Words from a .txt File & Filter Words Containing a Specific Character
Got it, let's first break down the issues in your current code that are causing those weird symbols and incorrect behavior:
- Incorrect file opening check: The line
if(read=fopen("slovnik_en.txt","r")==NULL)has operator precedence problems. The==runs before=, so you're assigning the comparison result (0 or 1) toreadinstead of the actual file pointer. You need parentheses around the assignment:if( (read = fopen("slovnik_en.txt", "r")) == NULL ). - Wrong array type: You declared
char *words[101][101];(a 2D array of character pointers) but tried to store single characters directly. You should use a 2D array of chars:char words[101][101];instead. - Reading characters instead of words: Using
fscanf(read,"%c",&words[i][j])reads every single character—including spaces, newlines, and tabs—so you're not splitting words at all. The%sformat specifier infscanfautomatically reads whole words by skipping whitespace. - Printing empty characters: When you initialize
words[i][j] = 0and immediately print it, you're outputting the null character, which shows up as a weird symbol in the console.
Here's the corrected code that meets your task requirements: it reads all words from the file, takes a character input from the console, and outputs every word containing that character:
#include <stdio.h> #include <string.h> int main() { FILE *file = fopen("slovnik_en.txt", "r"); if (file == NULL) { printf("ERROR: Could not open the file.\n"); return 1; } // Store up to 1000 words (adjust the size if your file has more words) char words[1000][101]; int word_count = 0; // Read all words from the file automatically splitting on whitespace while (fscanf(file, "%s", words[word_count]) == 1 && word_count < 999) { word_count++; } fclose(file); // Get the target character from user input char target; printf("Enter a character to filter words: "); scanf(" %c", &target); // The leading space skips leftover whitespace (like newlines) // Print all words that contain the target character printf("\nWords containing '%c':\n", target); for (int i = 0; i < word_count; i++) { // strchr checks if the character exists in the word if (strchr(words[i], target) != NULL) { printf("- %s\n", words[i]); } } return 0; }
Quick breakdown of how this works:
- Reading words:
fscanf(file, "%s", words[word_count])reads each word until it hits whitespace (space, newline, etc.), which is exactly how you split words from the text file. - Filtering words: The
strchrstandard library function checks if the target character exists in a word. It returns a pointer to the first occurrence of the character, orNULLif it's not found—perfect for our filtering logic. - Proper file handling: We close the file after reading to free system resources, and add a clear error message if the file can't be opened.
- Clean user input: The
scanf(" %c", &target)includes a leading space to skip any leftover whitespace (like if the user pressed enter accidentally), ensuring we capture the intended character correctly.
Just compile and run this code, make sure slovnik_en.txt is in the same directory as your executable, and it should work exactly as you need.
内容的提问来源于stack exchange,提问作者Danil
相关产品推荐
相关产品推荐

