?
Solved

display 20 characters on each side of a strstr result

Posted on 2003-03-19
5
Medium Priority
?
184 Views
Last Modified: 2010-04-01
i am doing a little app that requests a search term, opens a file, loops through the file looking for the search and displays each occurence of the term on screen.

after i find the term using:

    inFile.getline(buf, 299, '\n');
    char * pch;
    pch = strstr (buf,searchTerm);

i want to set the pointer 10 characters back in the file and then extend the length of the substring by the searchTerm and another 10 characters e.g.

nnnnnnnnnn searchTerm nnnnnnnnnn

so it is centred and we can see the text either side of the search term.

another problem i forsee is if the search term is found less than 10 characters from the start or end of the string in 'buf'
0
Comment
Question by:antonumia
  • 3
  • 2
5 Comments
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8167776
seekg() should do the trick.
(MS def)
istream& seekg( streamoff offset, ios::seek_dir dir );
to set it back 10 characters simply do something like:
inFile.seekg(-10,ios::cur);
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8167826
>> if the search term is found less than 10 characters >>from the start or end of the string in 'buf'
in this case you can check if(ios::cur - ios::beg > 10) or something like that.

if(ios::cur - ios::beg > 10)
//do the move back 10.
  inFile.seekg(-10, ios:cur);
else
//set to the beginning
  inFile.seekg(ios::beg);
0
 
LVL 4

Accepted Solution

by:
n_fortynine earned 200 total points
ID: 8167838
similarly if you don't want to go pass the end of your file you can check between ios::cur and ios::end
0
 
LVL 1

Author Comment

by:antonumia
ID: 8167944
thanks,

0
 
LVL 1

Author Comment

by:antonumia
ID: 8173908
btw: it didn't work until i cleared the stream state first.

e.g.
inFile.clear();
inFile.seekg(n,ios:beg);


0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question