need some help in files!

Hi! i'm a bit new in c++ so i'v got some question wish you to answer me...!
first of all i want my program to show the specific group of line of my file so i use this code for it:
using namespace std;
int main()
{
ifstream myfile;
myfile.open("test.txt");

cout<<"which line to start showing?";
string showline;
cin>>showline;
string line;




while(getline(myfile,line))
{
if(line==showline)
{
do
{
cout<<line<<endl;



}while(line!="javad");
}


}
myfile.close();


}

what is the problem of this code? can anyone give the correct one of this or another one for my question?

2.after that i want my program to delete specific lines of my file... i don't have any idea what to do... can anyone give the code?
thanks so much!
This }while(line!="javad"); will run either forever (since line doesn't change) or only once. What's the purpose of that line?


Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/
my first problem solved!
how can i delete specific lines of file?!
read the whole file, delete the parts you want to get rid off and then write to file.

if you just want to not use a few lines of the file, you can use ignore.
how can i delete specific lines of file?!
You can't.

What you can do is:

read all lines into a container (array). remove the line from the container. overwrite the original file with the content of the container
I write this code for delete specific lines but i think it has some problems guys would tell me what it is and how to fix it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using namespace std;
int main()
{
ifstream myfile;
myfile.open("test.txt");
ofstream exfile;
exfile.open("exfile.txt");
cout<<"which line to delete?";
string delline;
cin>>delline;
string line;
    while(getline(myfile,line))
{
    do
    {
        if(line!=delline)
        {
            exfile<<line<<endl;
            if(myfile.eof()) break;
            getline(myfile,line);
        }
    }while(!myfile.eof());

}
myfile.close();
exfile.close();
}


Thanks!
the inner loop doesn't make sense, it's already handled by the outer loop. you may omit line 19 and 20. they don't have any use when the inner loop is removed
sorry but i don't understand what you are talking about!
would you describe it more?
Thanks so so much!
Well:
1
2
3
4
5
6
7
8
    while(getline(myfile,line))
{
        if(line!=delline)
        {
            exfile<<line<<endl;
        }

}
yeah i know that's correct but i want to delete a group of lines! like this:
i have text file with this data:
aaa
bbb
ccc
ddd
fff
eee

when i enter :bbb
i want the program delete line of file from bbb to fff...!

how can i do that?
Topic archived. No new replies allowed.