Reading from a file with characters and numbers

I may be over thinking this. I need to open a file that has multiple lines that look something like this, " black box 100.01 33.5 78.93". What this shows is the coordinates of the black box. Lines will have different objects with different coordinates. I need to create a loop that will read this file and tell me when a black box is found by displaying a message. I don't need to know how to create this file. I don't need to display the entire file but rather have it search for black boxes. Some general guidance would be appreciated.
take a look at std::string and getline
You should start by breaking it down:

1. how do you recognize a single line?
2. How can you retrieve that line?

if you use a string, you can store that line. '\n' is a newline character, so we can just read until we hit that character. You can use the member function .good() from std::istream to tell if the file buffer is still valid.

That should give you everything you need.
I forgot to mention that I'm in an introductory c++ class. So I do not know some commands or other ways to do this that some of you more experienced people will know. I just learned about functions if that gives you a better understanding. I'm thinking I need to make my own file for testing purposes. But for this the professor will be prompted to enter his own file name which has multiple locations to be found.

Is there a way to scan this file for the locations I'm looking for and have it display " black box found" and also show the location? My text book doesn't go into details about including loops and if statements within a file search. If that makes sense?
Last edited on
Topic archived. No new replies allowed.