?
Solved

display 20 characters on each side of a strstr result

Posted on 2003-03-19
5
Medium Priority
?
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

800 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