How do i constantly keep checking for specific characters in a text file till there are no more specific characters left.

this way i can keep displaying text file logos on my application until there are none left in the text file being searched by c++ :)


i did have a look around on the string class and f stream but i really don't know what it is i'm so posed to do to achieve the results i'm after.

thank you for taking the time to read this.

Dani AI

Generated

asked for a way to keep scanning a text file for a set of characters (for example, all vowels) and keep acting until none remain. was right to point toward STL and file I/O; below are two concrete, idiomatic patterns that do exactly that and a few practical caveats.

A simple, easy-to-follow approach: read the file into a std::string, then loop with std::string::find_first_of to find any target character, handle it (display a logo), remove it, and repeat until no matches remain.

#include <fstream>
#include <string>
#include <iostream>
#include <iterator>

int main() {
    const std::string targets = "aeiouAEIOU"; // characters to find
    std::ifstream in("input.txt");
    if (!in) return 1;

    std::string content((std::istreambuf_iterator<char>(in)),
                         std::istreambuf_iterator<char>());

    size_t pos;
    while ((pos = content.find_first_of(targets)) != std::string::npos) {
        char found = content[pos];
        // display the logo or perform any action for 'found'
        std::cout << "Found '" << found << "' at " << pos << '\n';
        content.erase(pos, 1); // remove that occurrence so the loop can continue
    }

    std::ofstream out("clean.txt");
    out << content;
}

If performance is important (large files) and the action does not need to happen per-found-character, remove all matches in one pass using the erase–remove_if idiom (include <algorithm>):

content.erase(std::remove_if(content.begin(), content.end(),
                             [&](char c){ return targets.find(c) != std::string::npos; }),
              content.end());

Notes and troubleshooting: avoid loading huge files into memory — process line-by-line or stream chunks. If the file is being written by another process (a live log), either poll for new data or use platform file-change notifications (inotify on Linux, ReadDirectoryChangesW on Windows). For non-ASCII text (UTF-8), per-byte char checks will fail for multibyte characters; use a proper Unicode-aware parser. For best responsiveness when displaying a logo for each match, the find/erase loop is simple and clear; for throughput, prefer single-pass filtering or building an output string while skipping matches.

Recommended Answers

All 3 Replies

Please be more specific in the description of what you are trying to do. The way I read the first sentence it looks like you want to find a set of characters in a text file, say all the vowels in a piece of text like "eenie meenie minie mo". What that has to do with the second line of the post I don't understand.

thats exactly what i want but i want c++ to constantly look for it till there is no existence of set characters :)

Start by determining what tools you have available. I believe the STL library has a search function in the string class that allows you to search a given string for a given set of characters, but I don't use the STL extensively so don't quote me. You could use the strtok() function. I know that allows you to perform that type of search. But since I do this for pleasure and not work you could make up your own function like I would.

1)Declare an array to hold all the characters you want to search for.
2)use a nested loop to search each char in the string for each char in the array.
3)If you find on of the search char, do something, otherwise why bother searching.

4)When you can do that for a fixed string allow user to enter string to search and characters to search for.

5)When you can do that then learn how to work with files to read either the entire file at once (if it insn't gargantuan), one line (or some ,other convenient way to define a string, say a paragraph at a time or whatever), or a single char at a time.

6)Once you can read files comfortably, then obtain the material you want from the file in the format you want and apply the search algorithm you have created to do with the file contents what you want.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.