Hey, thanks in advance for any help!

I need to search a file for a specific word starting on a newline then skip a SPACE and read the next word into a char array or string, and then skip a word and read the next word, stopping at the next semicolon found...

Now, can I use the Space character as a delimiter? Or semicolon, or a word and space combination?

Specifically, I need to grab a couple unknown words that are next to known words, in unknown positions in a file, and set those words as strings or something, then use them later.

For example:

File data................
File data................
FindThisWord GetThisWord SkipThisWord GetThisOtherWord;
File data................
File data................

The idea is solid in my head. However, I have absolutely NO idea how to implement this...

Any ideas/suggestions? Thanks again for the help!

Dani AI

Generated

A practical, low-friction approach is: read the file line by line, detect the line whose first token equals your search word, then stream-tokenize that line and pick the tokens you need until you hit the semicolon. This preserves the "start of line" information that 's single-token loop loses and implements the line-focused idea suggested.

Try this pattern: read each line with std::getline, put the line into a std::istringstream, extract the first token and compare it to the search term, then >> subsequent tokens to capture/skip as needed. Watch out for a trailing ; attached to the last token (strip it before using the word) and for Windows CR (\r) at the end of a line.

Example sketch:

#include <fstream>
#include <sstream>
#include <string>

std::ifstream in("input.txt");
std::string line, first, tok;

while (std::getline(in, line)) {
    std::istringstream iss(line);
    if (!(iss >> first)) continue;            // empty line
    if (first != searchTerm) continue;        // not the line we want

    // capture first word after the search term
    if (iss >> tok) {
        std::string got1 = tok;

        // skip one word, then capture the next
        if (iss >> tok && iss >> tok) {
            if (!tok.empty() && (tok.back() == ';' || tok.back() == '\r'))
                tok.pop_back();
            std::string got2 = tok;
            // got1 and got2 are now available
        }
    }
}

Troubleshooting notes: use line.find(searchTerm) == 0 if the term must be at column 0 (no leading spaces). If tokens may include punctuation or quotes, sanitize them (strip punctuation or handle quotes) before use. For more complex or variable formats consider std::regex or a small state-machine parser, but the getline + istringstream approach is simple and robust for the example you posted.

Recommended Answers

All 6 Replies

If you use fstream its >> operator will skip all spaces and tabs for you, so that all you get are words.

ifstream in("in.txt");
std::string word;
while( in >> word)
{
    // do something with this word
}

The problem with AD's solution is you never really know when you're at the beginning of a line.

You can:
Read a complete line.
Test the beginning for the word in question.
If found, there are many ways to 'parse' the line.
1) looking at each character and dealing with SPACEs and non-SPACEs as appropriate.
2) Use stringstreams to parse the line
3) use the strxxx functions.

Ok, this is what I have so far...

int main()
{
   char inFileName[256];
   char sterm[256];
   string FileData;
   ifstream inFile;

// Get filename and Search Term
   cout << "Enter name of inFile: " << endl;
   cin.getline(inFileName,256);
   cout << "Enter search term: " << endl;
   cin.getline(sterm,256);

// Search inFile for search term
   inFile.open(inFileName);

   while(inFile >> FileData)
   {
      cout << FileData;
   }

    inFile.close();
    return 0;
}

The above only serves to store all the file data into a string called FileData, then writes it out on the screen... So basically, I have it set up to be able to read the data (thanks to AD), but I still need to search for the words. WaltP got my mind going in the right direction, but I don't really know how to implement any of that.

So I think I need to take this step by step... Start out with getting the line with the search term on it, and priting out that line. Any ideas on that?

Once I get the line printed out succesfully, we can figure out how to get the words I need out of the line.

Thanks again for the help. I'm just starting out with C++ (and programming in general), so sorry for all the questions.

I think a good start would be to read line by line to find the first word... once again, I don't know where to start with that... maybe using istream& seekg() or isteam& read() in some way?

So I think I need to take this step by step... Start out with getting the line with the search term on it, and priting out that line. Any ideas on that?

Once I get the line printed out succesfully, we can figure out how to get the words I need out of the line.

This is an excellent idea. You give it a try and we'll help when you get stuck. But you have to get stuck first. Look up the methods that can be used to compare strings and get substrings.

Thanks, will do. I'll post back if I get stuck. I don't want the whole thing spoon fed to me... I figure it's better to learn by trial and error, if you want to learn anything.

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.