Storing Words from Text File to an Array in C Programming
In C programming, it is common to need to store words from a text file into an array for various operations such as processing, sorting, or analyzing data. This article provides a step-by-step guide on how to implement this using basic C functions and memory management techniques.
Introduction to Storing Words from a Text File
This process involves several key steps: opening the file, reading the words, storing them, and finally, managing the memory. The example provided will cover these steps in a clear and concise manner.
Steps to Store Words from Text File to Array in C
Step 1: Open the File
The first step is to open the text file in read mode. This is achieved using the fopen function.
Step 2: Read the Words
Once the file is opened, the next step is to read the words from the file. This can be done using functions like fscanf or fgets. In this example, we will use fscanf.
Step 3: Store the Words
The words need to be stored in an array of character pointers. Each pointer will hold a dynamically allocated memory space for the word to ensure it has enough space to store the string including the null terminator.
Step 4: Close the File
After reading all the words and storing them in the array, the file must be closed using the fclose function.
Example Code in C
Here is a simple example demonstrating the above steps:
Header Inclusions
#include stdio.h#include stdlib.h#include string.h
Define Constants
#define MAX_WORDS 100 // Maximum number of words#define MAX_WORD_LENGTH 50 // Maximum length of each word
Main Function
int main() { FILE *file; // Pointer to hold file handle char words[MAX_WORDS][MAX_WORD_LENGTH]; // Array to hold words (using string arrays) char buffer[MAX_WORD_LENGTH]; int count 0; // Open the file for reading file fopen("input.txt", "r"); if (file NULL) { perror("Error opening file"); return EXIT_FAILURE; } // Read words from the file while (count MAX_WORDS fscanf(file, "Is", buffer) ! EOF) { strcpy(words[count], buffer); // Copy the word into the array count ; } // Close the file fclose(file); // Print the stored words printf("Stored words: "); for (int i 0; i count; i ) { printf("%s ", words[i]); } return EXIT_SUCCESS;}
Explanation of the Example Code
File Handling
fopen "input.txt", "r": Opens the file named input.txt in read mode.fclose file: Closes the file after reading.Reading Words
fscanf(file, "Is", buffer): Reads a word from the file into buffer. The Is format specifier ensures that a maximum of 48 characters are read, leaving 1 additional space for the null terminator.Storing Words
strcpy(words[count], buffer): Copies the word from buffer to the corresponding location in the words array.Memory Management
In this example, no dynamic memory allocation is required. However, if you were to use a different approach, you would need to allocate and free memory to prevent memory leaks.
Conclusion
Storing words from a text file into an array in C is a fundamental task often encountered in text processing applications. By following the steps and understanding the usage of C functions, you can efficiently manage text data and perform various operations on it.