943,929 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5225
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 14th, 2008
0

Replace a Word in a .txt file

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Posting Pro
Jennifer84 is offline Offline
563 posts
since Feb 2008
Mar 14th, 2008
0

Re: Replace a Word in a .txt file

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Mar 14th, 2008
0

Re: Replace a Word in a .txt file

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.

C++ Syntax (Toggle Plain Text)
  1. Line.substr(Line.rfind("Number2"))


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.
Reputation Points: 10
Solved Threads: 1
Posting Pro
Jennifer84 is offline Offline
563 posts
since Feb 2008
Mar 14th, 2008
0

Re: Replace a Word in a .txt file

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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Mar 14th, 2008
0

Re: Replace a Word in a .txt file

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
C++ Syntax (Toggle Plain Text)
  1. std::string Line = "abc Number[1]";
  2. int StartIndex = 0;
  3.  
  4. StartIndex = Line.find("Number[");


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.
Reputation Points: 10
Solved Threads: 1
Posting Pro
Jennifer84 is offline Offline
563 posts
since Feb 2008
Mar 14th, 2008
0

Re: Replace a Word in a .txt file

>>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
C++ Syntax (Toggle Plain Text)
  1. size_t pos = line.find('[');
  2. if( pos != string::npos)
  3. {
  4. int x = atoi( line.substring(pos+1).c_str());
  5. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Mar 14th, 2008
0

Re: Replace a Word in a .txt file

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)

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Posting Pro
Jennifer84 is offline Offline
563 posts
since Feb 2008
Mar 14th, 2008
0

Re: Replace a Word in a .txt file

Click to Expand / Collapse  Quote originally posted by Jennifer84 ...
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 "["

C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Mar 14th, 2008
0

Re: Replace a Word in a .txt file

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"

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Posting Pro
Jennifer84 is offline Offline
563 posts
since Feb 2008
Mar 15th, 2008
0

Re: Replace a Word in a .txt file

Click to Expand / Collapse  Quote originally posted by Jennifer84 ...
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"

C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Student Grades & Structs
Next Thread in C++ Forum Timeline: depreciation program....help please....





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC