Replace a Word in a .txt file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 517
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

Re: Replace a Word in a .txt file

 
0
  #11
Mar 15th, 2008
I didn´t know that the second argument was the length so now I know

I have change the code a bit though my example perheps wasn´t the best.
I do face a problem that I dont know how to solve really.
If I put the first occurence in the string with brackets like this: a4545l5l[12]
Then this code will not work to extract the number "51".
This is because I am .find("]") wich will be the first one in the string Line;
and as it takes this one into considiration, this will meen the length at the second argument in Line.find.

Is it not possible to find the next occurence of a character and then find("]") wich comes after the string stringToFind ?


  1. std::string Line = "a4545l5l[12] 1Number2[51] 2Number[1]";
  2. int index;
  3. string stringToFind = "1Number2[";
  4. index = Line.find(stringToFind);
  5. index = index + stringToFind.length ();
  6.  
  7. std::string Number;
  8.  
  9. Number = Line.substr(index, (Line.find("]") - index));
  10.  
  11. fout1 << Number;
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Replace a Word in a .txt file

 
0
  #12
Mar 15th, 2008
Originally Posted by Jennifer84 View Post
Is it not possible to find the next occurence of a character and then find("]") wich comes after the string stringToFind ?
The find() function can be told where to start the search from, see below
  1. string Line = "a4545l5l[12] 1Number2[51] 2Number[1]";
  2. string stringToFind = "1Number2[";
  3.  
  4. string::size_type startPos = Line.find(stringToFind);
  5. startPos += stringToFind.length();
  6.  
  7. string::size_type endPos = Line.find("]", startPos);
  8.  
  9. string Number = Line.substr(startPos, endPos - startPos);
  10.  
  11. cout << Number;

Remember that find() returns std::string::npos, if there is no match, so handle that case also in your code.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 517
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

Re: Replace a Word in a .txt file

 
0
  #13
Mar 15th, 2008
This did work fine and I did handle the scenario like this if there is no match for the stringToFind and it seems to work fine.

I am not really sure what ::size_type really meens ? The only thing I understand is that it is something that can handle any size of string.
Anyway this is a powerful way of searching strings. Thanks...

  1. string Line = "a4545l5l[12] 1Number2[51] 2Number[1]";
  2. string stringToFind = "1Number2[";
  3. string::size_type startPos = Line.find(stringToFind);
  4. startPos += stringToFind.length();
  5. string::size_type endPos = Line.find("]", startPos);
  6. string Number = Line.substr(startPos, endPos - startPos);
  7.  
  8. if( Line.find(stringToFind) != string::npos )
  9. {
  10. fout1 << Number;
  11. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC