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!

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.