We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,845 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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!

3
Contributors
5
Replies
12 Hours
Discussion Span
4 Months Ago
Last Updated
6
Views
peymankop
Light Poster
42 posts since Dec 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

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

peymankop
Light Poster
42 posts since Dec 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

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!

peymankop
Light Poster
42 posts since Dec 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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
Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 12

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0732 seconds using 2.71MB