Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search and read multiple lines from a file

Status
Not open for further replies.

nikes111

Technical User
May 3, 2004
11
US
I need to search a file for a name and display each line after that name until it comes to a '#' or some other specific character. How would I code this?

vipersig.jpg
 
Try starting with this (working;) sceleton:
Code:
#include <istream>
#include <string>

#include <fstream>
using namespace std;

void scan(istream& f, const char* hpat)
{
  string line;

  while (getline(f,line))
  {
    while (line.find(hpat) == string::npos 
        && getline(f,line))
      ;
    while (getline(f,line) && line[0] != '#')
      cout << line << endl;
  }
}

int main(int argc, char* argv[])
{
	ifstream f("test.txt");
	scan(f,"!");
	return 0;
}
It's 100% freeware, you may use/modify it as you wish...
 
Thanks ArkM, that was exactly what I needed.

vipersig.jpg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top