Replace a Word in a .txt file

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

Replace a Word in a .txt file

 
0
  #1
Mar 14th, 2008
I have a .txt file that contains this informaton:

Numb1 Number2[1] Numb3

Now there is 2 things that I am trying to do here. I dont know how to begin this.
First I will search if there is any Word in this file that is named:

Number2[1]

But the tricky thing here is that in this case there is a "1" inside the [].
The problem is that I will not know what number it is inbetween.
So initially what I am searching for is Number2 that consist of [] with an unknown number inside.

The 2 things I am trying to do is this then:

First: Get that number what it now will be and put that to a std::string.
Second: Is to output the file with one change and that is to replace in this case
"Number2[1]" to the word: "ABCDE" so the output file will look like this:

Numb1 ABCDE Numb3

Any idéas on how it could be possible to begin this ?
Last edited by Jennifer84; Mar 14th, 2008 at 7:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,150
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Replace a Word in a .txt file

 
0
  #2
Mar 14th, 2008
Its pretty easy to extract the number between the square brackets, but why? put the number in a std::string ?

std::string has a replace() method that you can use to replace one substring with another.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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
  #3
Mar 14th, 2008
Then number that I have found between the square brackets I will first then put to a std::string and after this... atoi it to an int to use in a calculation.

The first thing I might wonder is then to find Number2[?] in the .txt file that I ifstream.
The problem I think of is that I am serching for the whole "Number2[?] but don´t know what is in between the brackets. Perheps I have to in someway search for a
substr(0, rfind("[")+1) that is equal to "Number2" and then put this to a string and then substring that to find the number. Am I thinking right.

I suppose I will read each line and put it to a "string" and search this string for substr Number2 in anyway.
I am not really sure how to exactly practically begin how to do.

The first goal should be to just find that number inside the brackets.
I think it could be done if I put the whole line '\n' into a string and then put the startindex to this and then continue find the number.
Is this the way it could be done ?
Thanks.

  1. Line.substr(Line.rfind("Number2"))


Originally Posted by Ancient Dragon View Post
Its pretty easy to extract the number between the square brackets, but why? put the number in a std::string ?


std::string has a replace() method that you can use to replace one substring with another.
Last edited by Jennifer84; Mar 14th, 2008 at 9:17 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,752
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 491
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Replace a Word in a .txt file

 
0
  #4
Mar 14th, 2008
How about using the "find" function in the string class to search for an occurrence of "Number2["?

I think you are on to the same idea with rfind. find will return the index of the 'N' in "Number2[". Add the number of characters in "Number2[" and you are at the index of the number inside the brackets and you can extract that number. I'm sure using rfind would also work equally well. This page link has a good example of how to use "find".

http://www.cplusplus.com/reference/s...ring/find.html
Last edited by VernonDozier; Mar 14th, 2008 at 9:39 pm. Reason: Bad grammar in original post.
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
  #5
Mar 14th, 2008
I am not sure if I am doing right here. I am trying to find the startindex like this:
I think I have missed something. When doing this StartIndex return: -1
  1. std::string Line = "abc Number[1]";
  2. int StartIndex = 0;
  3.  
  4. StartIndex = Line.find("Number[");


Originally Posted by VernonDozier View Post
How about using the "find" function in the string class to search for an occurrence of "Number2["?

I think you are on to the same idea with rfind. find will return the index of the 'N' in "Number2[". Add the number of characters in "Number2[" and you are at the index of the number inside the brackets and you can extract that number. I'm sure using rfind would also work equally well. This page link has a good example of how to use "find".

http://www.cplusplus.com/reference/s...ring/find.html
Last edited by Jennifer84; Mar 14th, 2008 at 9:59 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,150
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Replace a Word in a .txt file

 
0
  #6
Mar 14th, 2008
>>substr(0, rfind("[")+1)
Don't even attempt to do that because it might crash the program if rfind returns string::npos (-1). Better to split it into two lines so that you can do error checking
  1. size_t pos = line.find('[');
  2. if( pos != string::npos)
  3. {
  4. int x = atoi( line.substring(pos+1).c_str());
  5. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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
  #7
Mar 14th, 2008
I think I have to take it from the beginning. I mix things up I think.
I will take a straight example. I have the string Line and want to know the index of "[" in
1Number[1].
If I do like I have done below I will have the index 4 wich is the beginning of "1Number["
I am unsure how to continue to I reach the "["
I have done like below but index2 will have the substring: "1] 2Number"

I wonder why that happens since I have set index (8,10)

  1. std::string Line = "abc 1Number[1] 2Number[1]";
  2. string index;
  3. index = Line.substr(Line.find("1Number["));
  4.  
  5. string index2;
  6. index2 = index.substr(8,10);
Last edited by Jennifer84; Mar 14th, 2008 at 10:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,752
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 491
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Replace a Word in a .txt file

 
0
  #8
Mar 14th, 2008
Originally Posted by Jennifer84 View Post
I think I have to take it from the beginning. I mix things up I think.
I will take a straight example. I have the string Line and want to know the index of "[" in
1Number[1].
If I do like I have done below I will have the index 4 wich is the beginning of "1Number["
I am unsure how to continue to I reach the "["

  1. std::string Line = "abc 1Number[1] 2Number[1]";
  2. int index;
  3. index = Line.find("1Number[");
Here was my idea. This only works for one digit indexes, but it takes you to the index of the STRING that is the first digit of the integer inside of the brackets. You'd have to extract that number and convert it to an integer, but this delivers you to the correct spot in the string:
  1. #include <string>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main ()
  6. {
  7. std::string Line = "abc 1Number2[5] 2Number[1]";
  8. int index;
  9. string stringToFind = "Number2[";
  10. index = Line.find(stringToFind);
  11. index = index + stringToFind.length ();
  12. cout << "Here is the index: " << Line[index] << endl;
  13. return 0;
  14. }

This will display 5.

I think this is what you are looking for, or at least part of it. Is it, or am I misinterpreting the goal?
Last edited by VernonDozier; Mar 14th, 2008 at 10:49 pm.
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
  #9
Mar 14th, 2008
Thank you. That did work and I understand the logic. I have tried for a while now to experiment on this as this as you said only could find one digit number inside the []
So I am trying to develop it to find everyting until the next "]"
I have tried out this example but this finds the last "]" on that line and not next after the number 51 so it extracts: "51] 2Number[1]" and not only then "51"

  1. std::string Line = "abc 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("]"));
  10.  
  11. fout1 << Number;
Last edited by Jennifer84; Mar 14th, 2008 at 11:59 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,752
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 491
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Replace a Word in a .txt file

 
0
  #10
Mar 15th, 2008
Originally Posted by Jennifer84 View Post
Thank you. That did work and I understand the logic. I have tried for a while now to experiment on this as this as you said only could find one digit number inside the []
So I am trying to develop it to find everyting until the next "]"
I have tried out this example but this finds the last "]" on that line and not next after the number 51 so it extracts: "51] 2Number[1]" and not only then "51"

  1. std::string Line = "abc 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("]"));
  10.  
  11. fout1 << Number;
It actually is finding the first ']', not the second. Here is the prototype for substr:
  1. string substr ( size_t pos, size_t n) const;
n in the above prototype is NOT an index of the string. Rather it is the number of characters in the substring. In your case above, the '5' is at index 13 of the string and ']' is at index 15. You want the substring to be two characters. You are specifying that it will be 15 characters. Hence you get all the extra characters. Try this modification:
  1. std::string Line = "abc 1Number2[51] 2Number[1]";
  2. int index;
  3. string stringToFind = "Number2[";
  4. index = Line.find(stringToFind);
  5. index = index + stringToFind.length ();
  6. std::string Number;
  7. int rightBracketIndex = Line.find("]");
  8. int numCharsInNumber = rightBracketIndex - index;
  9. Number = Line.substr(index, numCharsInNumber);
  10. fout1 << Number;
Last edited by VernonDozier; Mar 15th, 2008 at 12:42 am.
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