1.11M Members

need some help in files!

 
0
 

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!

 
1
 

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.

 
0
 

in delete file... how can i write the second idea code?!("you can read the whole file into memory, remove the lines that you want and then over-write the original file with edited contents.") would give me the piece of code?!

 
1
 

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;
}
 
0
 

Thanks... but what this code really do?! it create a file named "b.txt" with nothing in it...! would you give me instructions how this code works?
thanks!

 
0
 

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
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article