| | |
Replace a Word in a .txt file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 517
Reputation:
Solved Threads: 1
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 ?
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.
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.
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.
•
•
Join Date: Feb 2008
Posts: 517
Reputation:
Solved Threads: 1
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.
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)
Line.substr(Line.rfind("Number2"))
Last edited by Jennifer84; Mar 14th, 2008 at 9:17 pm.
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
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
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.
•
•
Join Date: Feb 2008
Posts: 517
Reputation:
Solved Threads: 1
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
I think I have missed something. When doing this StartIndex return: -1
C++ Syntax (Toggle Plain Text)
std::string Line = "abc Number[1]"; int StartIndex = 0; 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.
>>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
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)
size_t pos = line.find('['); if( pos != string::npos) { int x = atoi( line.substring(pos+1).c_str()); }
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.
•
•
Join Date: Feb 2008
Posts: 517
Reputation:
Solved Threads: 1
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)
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)
std::string Line = "abc 1Number[1] 2Number[1]"; string index; index = Line.substr(Line.find("1Number[")); string index2; index2 = index.substr(8,10);
Last edited by Jennifer84; Mar 14th, 2008 at 10:50 pm.
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
•
•
•
•
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)
std::string Line = "abc 1Number[1] 2Number[1]"; int index; index = Line.find("1Number[");
C++ Syntax (Toggle Plain Text)
#include <string> #include <iostream> using namespace std; int main () { std::string Line = "abc 1Number2[5] 2Number[1]"; int index; string stringToFind = "Number2["; index = Line.find(stringToFind); index = index + stringToFind.length (); cout << "Here is the index: " << Line[index] << endl; return 0; }
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.
•
•
Join Date: Feb 2008
Posts: 517
Reputation:
Solved Threads: 1
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"
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)
std::string Line = "abc 1Number2[51] 2Number[1]"; int index; string stringToFind = "1Number2["; index = Line.find(stringToFind); index = index + stringToFind.length (); std::string Number; Number = Line.substr(index, Line.find("]")); fout1 << Number;
Last edited by Jennifer84; Mar 14th, 2008 at 11:59 pm.
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
•
•
•
•
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)
std::string Line = "abc 1Number2[51] 2Number[1]"; int index; string stringToFind = "1Number2["; index = Line.find(stringToFind); index = index + stringToFind.length (); std::string Number; Number = Line.substr(index, Line.find("]")); fout1 << Number;
C++ Syntax (Toggle Plain Text)
string substr ( size_t pos, size_t n) const;
C++ Syntax (Toggle Plain Text)
std::string Line = "abc 1Number2[51] 2Number[1]"; int index; string stringToFind = "Number2["; index = Line.find(stringToFind); index = index + stringToFind.length (); std::string Number; int rightBracketIndex = Line.find("]"); int numCharsInNumber = rightBracketIndex - index; Number = Line.substr(index, numCharsInNumber); fout1 << Number;
Last edited by VernonDozier; Mar 15th, 2008 at 12:42 am.
![]() |
Similar Threads
- Open, search, replace data, save, close .txt file (VB.NET)
- Replace (Shell Scripting)
- Laptop has been infected with Spyware! PLEASE HELP! (Viruses, Spyware and other Nasties)
- PLease help Spyware (Viruses, Spyware and other Nasties)
- Please Help, have been working on this Program for awhile and dissapointed in Results (C++)
- Shell Script Question (Shell Scripting)
- how to move to the second line in C++ .txt file reading? (C++)
- Python - Importing Data with a Class (Python)
- "Virus Alert" on my taskbar!! (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: Student Grades & Structs
- Next Thread: depreciation program....help please....
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






