I trying to write a program that uses a getline to read from a txt file.
I need it to read from the file until it reaches a line that has "$$$" in it.

I can get it to read in the file like this no problem.

//assuming all variables have been declared

while(inFile.peek() != '\r' && inFile.peek() != '\n')
	{
        
        /* gets the next line and sets it as the bike */
		getline(inFile, line);
		buffer << line;
		buffer >> bike;
		buffer.clear();

		/* gets the next line and sets it as the car */
		getline(inFile, line);
		buffer << line;
		buffer >> car;
		buffer.clear();

		/* gets the first line and sets it as the jeep */
		getline(inFile, line);
		buffer << line;
		buffer >>jeep;
		buffer.clear();

		if(inFile.eof()) break;

    }

My data file looks like this:

huffy
corvette
jeep
$$$
dog
cat


I want it to break from the while loop when it reaches the "$$$".
I have a particular function that I will perform on the
"huffy corvette jeep" values, then I want it to stop reading the values when the "$$$" is found, so I can read in the " dog cat" in the same way, but use a different function on them.

any thought??

Recommended Answers

All 4 Replies

#include <string>
#include <fstream>
#include <iostream>
#include <algorithm>

int main() {
	const std::string pattern("$$$");
	std::ifstream file("d:\\test.txt");
	if ( !file ) {
		std::cerr << "Can`t open file!" << std::endl;
		return 1;
	}
	std::string line;
	
	while ( std::getline(file, line) ) {
		if ( line.empty() ) continue;
		if ( std::search(line.begin(), line.end(), pattern.begin(), pattern.end()) != line.end() ) break;
	}
	
	return 0;
}

Thanks for the reply, but I think that wont work with the logic I am using. Maybe I am wrong.
I'm trying to use the code like this but is says it can't convert it to bool, in my error message.

Here is my modified code.

//assuming all variables have been declared

ifstream	inFile;

string pattern("$$$");
inFile.open("prog4.dat");
if(inFile.fail())
	{
		cerr << "Can't open data file" << endl;
		exit(1);
	}

while(inFile.peek() != '\r' && inFile.peek() != '\n')

{
/* gets the next line and sets it as the bike */

getline(inFile, line);
buffer << line;
buffer >> bike;
buffer.clear();

/* gets the next line and sets it as the car */
getline(inFile, line);
buffer << line;
buffer >> car;
buffer.clear();

/* gets the first line and sets it as the jeep */
getline(inFile, line);
buffer << line;
buffer >>jeep;
buffer.clear();

if(inFile.eof()) break;
if(pattern.begin())break;
}

My data file looks like this:

huffy
corvette
jeep
$$$
dog
cat


I want it to break from the while loop when it reaches the "$$$".
I have a particular function that I will perform on the
"huffy corvette jeep" values, then I want it to stop reading the values when the "$$$" is found, so I can read in the " dog cat" in the same way, but use a different function on them.

any thought??

just do this :

string input;
	string s = "$$$";

	ifstream iFile("ReadMe.txt");

	while(getline(iFile,input))
	{
		if(input == s) break;
		else cout<<input<<endl;
	}

Haven't tried it but that logic should work.

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.