TechTorch

Location:HOME > Technology > content

Technology

How to Write a C Program to Find and Save Unique Words from a File

July 13, 2025Technology3696
How to Write a C Program to Find and Save Unique Words from a File Thi

How to Write a C Program to Find and Save Unique Words from a File

This tutorial will guide you through the process of writing a C program that reads words from a text file, identifies unique words, and stores them in another file. We will explore the necessary steps, code examples, and relevant concepts to ensure your program works seamlessly.

Steps to Write the C Program

To develop a C program that accomplishes this task, follow these steps:

1. Read Words from the Input File

Open the input file and extract its content. This step involves initializing file streams and reading each line or word from the file.

2. Store Unique Words

Utilize a data structure like std::set that inherently manages unique elements. This will help eliminate duplicate words automatically.

3. Write Unique Words to the Output File

Open the output file, write the unique words to it, and ensure each word is properly formatted and terminated with a newline.

Complete Example in C

Here is a complete example of how to write a C program for this task:

#include iostream
#include fstream
#include set
#include string
template typename T
class UniqueWords {
public:
    void readFromFile(const std::string inputFileName) {
        std::ifstream inputFile(inputFileName);
        if (!inputFile) {
            std::cerr  Error: Unable to open input file.
;
            return;
        }
        std::string word;
        while (inputFile  word) {
            (word);
        }
        ();
    }
    void writeToFile(const std::string outputFileName) {
        std::ofstream outputFile(outputFileName);
        if (!outputFile) {
            std::cerr  Error: Unable to open output file.
;
            return;
        }
        for (const auto uniqueWord : uniqueWords) {
            outputFile  uniqueWord  
;
        }
        ();
    }
private:
    std::setstd::string uniqueWords;
};
int main() {
    // Input and output file names
    std::string inputFileName  input.txt;
    std::string outputFileName  output.txt;
    UniqueWordsstd::string uniqueWordsObj;
    (inputFileName);
    uniqueWordsObj.writeToFile(outputFileName);
    std::cout  Unique words written to output file successfully.
;
    return 0;
}

Explanation

Includes and Namespaces

The program includes necessary headers like iostream, fstream, set, and string. It uses the standard namespace to avoid qualification.

File Handling

The program opens the input file, checks if it opens successfully, and reads each word into the std::set, which automatically manages uniqueness.

Reading Words

Each word is read from the input file using the extraction operator . This operator extracts words separated by whitespace until the end of the line or file.

Writing to Output

After reading all the words, the program writes each unique word to the output file, followed by a newline character.

Closing Files

The input and output files are closed properly after their operations are completed.

Notes

Make sure to replace input.txt and output.txt with the actual paths of your input and output files.

This program assumes that words are separated by whitespace. If you have punctuation or other delimiters between words, you might need to modify the word extraction logic accordingly.

To optimize this program for different use cases, you can consider implementing additional features like handling punctuation or tuning the performance based on input file size and structure.

Conclusion

By following the steps and code examples provided in this tutorial, you can successfully write a C program that reads unique words from a file and writes them to another file. This skill is valuable for various text processing tasks and can be further expanded to handle more complex requirements.