| | |
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 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 ?

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 ?
C++ Syntax (Toggle Plain Text)
std::string Line = "a4545l5l[12] 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("]") - index)); fout1 << Number;
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
•
•
•
•
Is it not possible to find the next occurence of a character and then find("]") wich comes after the string stringToFind ?
C++ Syntax (Toggle Plain Text)
string Line = "a4545l5l[12] 1Number2[51] 2Number[1]"; string stringToFind = "1Number2["; string::size_type startPos = Line.find(stringToFind); startPos += stringToFind.length(); string::size_type endPos = Line.find("]", startPos); string Number = Line.substr(startPos, endPos - startPos); cout << Number;
Remember that find() returns std::string::npos, if there is no match, so handle that case also in your code.
•
•
Join Date: Feb 2008
Posts: 517
Reputation:
Solved Threads: 1
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...
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...
C++ Syntax (Toggle Plain Text)
string Line = "a4545l5l[12] 1Number2[51] 2Number[1]"; string stringToFind = "1Number2["; string::size_type startPos = Line.find(stringToFind); startPos += stringToFind.length(); string::size_type endPos = Line.find("]", startPos); string Number = Line.substr(startPos, endPos - startPos); if( Line.find(stringToFind) != string::npos ) { fout1 << Number; }
![]() |
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 application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





