You will have an infinite loop in this program if you ask for any line that isn't "javad" and that line is found in the file.
You should have another call to getline inside the inner do{ ... }while loop. Somewhere between line 21 and line 25.
If you want to delete some lines from a text file, you can write the file out to another file and miss out the lines that you want. Or, you can read the whole file into memory, remove the lines that you want and then over-write the original file with edited contents.
ravenous
Practically a Master Poster
681 posts since Jul 2005
Reputation Points: 286
Solved Threads: 111
Skill Endorsements: 9
Well, to answer your question we'll have to clarify some things: memory items are different from the persistent ones (those who are stored on the disk). If the file isn't that big, you can store it, line by line, into the memory, using an appropriate container.
Here's a little example:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main(){
ifstream fin ("b.txt", ios::in);
string line;
vector<string> lines;
//add lines to memory
if (fin.good())
while (getline(fin, line)) lines.push_back(line);
//file is in memory.
//now you traverse the vector lines, for a specific line/lines and change that content
string modify="other", finds="something";
for (size_t i=0;i<lines.size();i++){
size_t found=line.find(finds);
if (found!=string::npos)
//we modify the line
line.replace(line.begin()+found, line.begin()+found+finds.size(), modify);
}
//we write back to the file:
ofstream out("b.txt", ios::out);
for (size_t i=0;i<lines.size();i++)
out<<lines[i]<<endl;
return 0;
}
Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 12
Supposedly you have a file called b.txt with 4 lines:
aaaa
something
bbbb
something
now you apply the code posted above, and the output, in b.txt should be:
aaaa
other
bbbb
other
Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 12