Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to delete data from a file in c++

Posted on 2007-10-10
46
Medium Priority
?
5,239 Views
Last Modified: 2012-08-14
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <vector>
#include<ctime>
using namespace std;

int ism_countstr(const char *buf, const char * str);
int main()
{
  string line, final_line, point_line, del_str, del_str2,source,delete_string,delete_string1;
  int del_len, del_pos,del_pos1,del_len1,str_len,count=0;
  int dim;
  string *points = new string[50];
  string point;
int pos,j,len=0;
const char* cmpstrvector;
char c;
std::string erasepoints;
cout<< "Enter the points to be deleted from the file:";

getline(cin,erasepoints);


cout << "Enter the file name: ";

cin>>source;

const char* filename = source.c_str();

cout<<"Enter the dimension:";
cin>>dim;

  ifstream main_file(filename, ios::in);
  while (getline(main_file, line, '\n'))  /// read the file from start to end line by line into a STL string object
  {
    final_line += line;
    final_line += " \n";
    cout<<"final_line :"<<final_line<<endl;
  }
  main_file.close();   /// close the file after reading is over.

  //ifstream point_file("point_file.txt", ios::in); //read point file into a STL string object
  //getline(point_file, point_line, '\n');    ////assumed that points to be deleted will be in a single line
  cout<<"point_line :"<<erasepoints<<endl;   ///that's why while loop is omitted here
  //point_file.close();

  str_len=erasepoints.length();
   cout<<"the string length is:"<<str_len;
  delete_string=erasepoints.substr(0,2*dim-1);
 
  del_len = delete_string.length();   ////find the length of the string to be deleted.
  cout<<"\ndelete_string is:"<<delete_string;
          
    del_pos = final_line.find(delete_string,0); ////search for the string to be deleted.
      
  if(del_pos >= 0 )
  {
        final_line.erase(del_pos,del_len);  ////if found, erase the string.
       
  }

  else
        cout<<"\n"<<erasepoints<<" is not found in main file."<<endl;
   
 
  cout<<"\n final_line after :"<<final_line<<endl;
  ofstream outfile(filename, ios::out);  ////now open the file to write the modified string into file and close it.
  outfile<<final_line;
  outfile.close();
  return 0;
}

I want to delete the points from the file. the file will contain numbers from 0 to 99. the points that should be deleted from the file should be given in the console. i can able to delete the points from the file but i want to divide the points into small substrings like

the points to delete from the file are like: 1 2 3 4 5 6 7 8 then if we give the dimension as 4

the sequence 1 2 3 4 should be deleted first and then it should search for 5 6 7 8 and delete that sequence..

0
Comment
Question by:trivenibomma
  • 22
  • 18
  • 5
  • +1
46 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 20051997
>> the points to delete from the file are like: 1 2 3 4 5 6 7 8 then if we give the dimension as 4
>> 
>> the sequence 1 2 3 4 should be deleted first and then it should search for 5 6 7 8 and delete that sequence..

Can you explain what you mean by this ? What do you mean by "dimension" ? What do you mean by "sequence" ? Do you want to remove only those blocks of values that match the sequence exactly ?


If this is for an assignment, can you show the exact assignment (word for word) ?
0
 

Author Comment

by:trivenibomma
ID: 20052257
its my project...but i dont have that kind of description...

but i can explain u clearly...

we are giving the points that have to be deleted from the file as 1 2 3 4 5 6 7 8

i m able to delete the sequence of points as 1 2 3 4 5 6 7 8

but i am adding one more attribute as dimension that means i want to delete the points as a substring of sequence of points 1 2 3 4 5 6 7 8 as

if the dimension is 4 then i need to delete the points from the file

as 1 2 3 4
n then 5 6 7 8 sequence of points from the file...

i think i explained it clearly...
0
 
LVL 4

Expert Comment

by:Ichijo
ID: 20052980
So you want to split up the sequence of characters to delete into quantities specified by the dimension?

It sounds like you need to loop through the code from where it sets 'erasepoints' until it erases the points from 'final_line', and each time through the loop it should change 'erasepoints' to be the next set of points to erase.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:trivenibomma
ID: 20053173
yaa....can u tell me how to do that....i m a beginner in c++...
 
can u write the code for that....
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20054733
>> i m a beginner in c++...  can u write the code for that....

Since you're a beginner trying to learn C++, you should really try to write the code yourself. It's the best way to learn.


I still don't understand what you want to do though. Say you have this file :

        1, 1, 2, 3, 4, 9, 10, 4, 9, 5, 6, 7, 8, 3, 10, 7, 8

and you want to remove the sequence 1, 2, 3, 4, 5, 6, 7, 8 with dimension 4 (as you said it) .... what would be the resulting file ?
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20054772
Do you wnt to delete multiple occurences of the string "1 2 3 4"?

What happens if the 'erasepoints' length is not dividable by 'dim', e. g. "1 2 3 4 5 6 7 8 9", dim = 5 ? Do you want to erase "6 7 8 9" as a second block then?

What happens for numbers >= 10? Your code assumes that with (dim*2 - 1) there is a well-defined subsequence. But look at "9 10 11 12 13 14 15 16 17" and dim=3. The first sequence would be "9 10 1" what hardly is what you wants, right?

What is the reason that you want to search for subsequences? Why not simply search for shorter sequences fom the beginning and let the user continue with a new search with an already read file contents?

Regards, Alex

0
 

Author Comment

by:trivenibomma
ID: 20057920
if the file is having like this:

1, 1, 2, 3, 4, 9, 10, 4, 9, 5, 6, 7, 8, 3, 10, 7, 8

the points should be deleted are 1 2 3 4 5 6 7 8

then the output will be for dimension 4 is 1, 9, 10, 4, 9, 3, 10, 7, 8

i will be doing the code but i need to submit it urgently....
0
 

Author Comment

by:trivenibomma
ID: 20057944
if the erasepoints is not divisible by the dimension then

we can ignore the points that are less than the dimension...

like 1 2 3 4 5 6 7 8 9
dim -5

then we need to delete 1 2 3 4 5 n we can ignore 6 7 8 9
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20059192
>> then the output will be for dimension 4 is 1, 9, 10, 4, 9, 3, 10, 7, 8

Ok. Now I understand :) Thanks for clarifying.

You can take the erasepoints string, and find the 4-th space character (in case dimension is 4). Then use string::substr to cut out that part of the string. Then you can use your existing logic to remove it from the file.
0
 

Author Comment

by:trivenibomma
ID: 20059546
yaa i got that idea...but i didnt able to implement that logic...
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20059576
>> but i didnt able to implement that logic...

How far did you get ? It's not much different than the code you have now, except that you need to extract the correct substring from the erasepoints string.
0
 

Author Comment

by:trivenibomma
ID: 20059619
i m able to count the number of spaces in the string but i dont know how to add it to my program...
0
 

Author Comment

by:trivenibomma
ID: 20059810
int count = 0;
for (int i = 0; i < str_len; ++i)
if (erasepoints[i]==' ')
++count;
cout<<"count"<<count;
i have added this code to count the number of spaces in the erasepoints..
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20059876
When you counted 4 spaces (in case the dimension is 4), you keep the i index, and use that to extract the first 4 values using string::substr (so, from the start of the string until the i index position).
0
 

Author Comment

by:trivenibomma
ID: 20060063
i m not getting how to do it...

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <vector>
#include<ctime>
using namespace std;

int main()
{
  string line, final_line, point_line, del_str, del_str2,source,delete_string;
  int del_len, del_pos,del_pos1,str_len=0;
  int dim;
  std::string erasepoints;
cout<< "Enter the points to be deleted from the file:";

getline(cin,erasepoints);



  cout << "Enter the file name: ";

cin>>source;

const char* filename = source.c_str();

cout<<"Enter the dimension:";
cin>>dim;

  ifstream main_file(filename, ios::in);
  while (getline(main_file, line, '\n'))  /// read the file from start to end line by line into a STL string object
  {
    final_line += line;
    final_line += " \n";
    cout<<"final_line :"<<final_line<<endl;
  }
  main_file.close();   /// close the file after reading is over.

  //ifstream point_file("point_file.txt", ios::in); //read point file into a STL string object
  //getline(point_file, point_line, '\n');    ////assumed that points to be deleted will be in a single line
  //cout<<"point_line :"<<point_line<<endl;   ///that's why while loop is omitted here
  //point_file.close();
str_len=erasepoints.length();
  int count = 0;
for (int i = 0; i < str_len; ++i)
if (erasepoints[i]==' ')
++count;
cout<<"count"<<count;


  del_len = erasepoints.length();   ////find the length of the string to be deleted.
  while(count=dim-1)
  {
  delete_string=erasepoints.substr(0,dim);
 
          
    del_pos = final_line.find(erasepoints,0); ////search for the string to be deleted.
      
  if(del_pos >= 0 )
  {
        final_line.erase(del_pos,del_len);  ////if found, erase the string.
       
  }

  else
        cout<<erasepoints<<" is not found in main file."<<endl;

  }
  cout<<"final_line after :"<<final_line<<endl;

  ofstream outfile(filename, ios::out);  ////now open the file to write the modified string into file and close it.
  outfile<<final_line;
  outfile.close();
  //point_file.close();
  return 0;
}

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20060139
As I said, stop the loop when you reach the 4-th space character. Then you have the index you can use with string::substr :

        http://www.cplusplus.com/reference/string/string/substr.html

0
 

Author Comment

by:trivenibomma
ID: 20060229
i know what aru you saying...but i m not able to write the code for that....
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20061373
>> but i m not able to write the code for that....

Why not ? What are you stuck at ? Which part ?
0
 

Author Comment

by:trivenibomma
ID: 20061651
how to add the index n to get the substring from it....plzzz help me out in this....
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20063521
Did you take a look at this link :

        http://www.cplusplus.com/reference/string/string/substr.html

Did you understand what substr does and how it works ? Then you know which values you need : namely the start position and the length of the substring you want to extract.

The start index for the loop gives you the start position.
The loop index value at the end of the loop gives you the length.

That's enough to get the correct substring from the original string.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20063734
Are you using Visual Studio? If yes you should/could intend your code properly by Ctrl + A and ALT + F8.

>>>> how to add the index n to get the substring from it....plzzz help me out in this....

string::substr has two arguments. First is the startposition (0-based) where the extraction should begin. Second is the length of the string to extract. So if you found out that the 4.th space character is at position 8 in the erasepoints string, you could extarct the substring by

     string sub = erasepoints.substr(0, 8);

instead of 0, 8 you can use integer variables as well.

If you would like to take the rest into another substring you would do

     string rest = erasepoints.substr(8);  

Omitting the second argument means "take the rest". If you would like to not taking the space char at position 8 you would do

     string rest = erasepoints.substr(9);  

And if you would want to take the next four numbers in the erasepoints string, you would need to find the 4ths space char in 'rest' string - say it is now at position 10 - and do the same with 'rest' what you done before with 'erasepoints'.

     string sub2 = rest.substr(0, 10);

Regards, Alex
     

 

0
 

Author Comment

by:trivenibomma
ID: 20068429
i can able to divide into substrings but i m not getting at which position i m getting the 4th space in the string....
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20068451
>> i can able to divide into substrings but i m not getting at which position i m getting the 4th space in the string....

Just use a loop to find 4 spaces. Stop the loop when the 4-th space is found. That will give you the index.
0
 

Author Comment

by:trivenibomma
ID: 20068764
i m writing like this to find the 4th space index...but i m not getting right....

void main()
{
string erasepoints;

cout<<"enter the erasepoints";
getline(cin,erasepoints);

int str_len=erasepoints.length();
int count=0;
for(int i=0;i<str_len;i++)
if(erasepoints[i]=' ')
{
      count++;
      
}
if(count=4)
            cout<<"i"<<i;

}

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20069027
First of all, you have to make sure that you use {}'s around the loop body, and put everything in there that you want to do in the loop.

Second, you can break out of a loop prematurely using break;
0
 

Author Comment

by:trivenibomma
ID: 20069373
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <vector>
#include<ctime>
using namespace std;

int main ()
{
 //string text = "abc def ghi jkl mno";

  string line, final_line, point_line, del_str, del_str2,source,delete_string,delete_string1;
  int del_len, del_pos,del_pos1,del_len1,str_len,rest;
  int dim;
  string *points = new string[50];
  string point;
int pos,j,len=0;
const char* cmpstrvector;
char c;
string erasepoints;
//int position;
cout<< "Enter the points to be deleted from the file:";

getline(cin,erasepoints);


cout << "Enter the file name: ";

cin>>source;

const char* filename = source.c_str();

cout<<"Enter the dimension:";
cin>>dim;

  ifstream main_file(filename, ios::in);
  while (getline(main_file, line, '\n'))  /// read the file from start to end line by line into a STL string object
  {
    final_line += line;
    final_line += " \n";
    cout<<"final_line :"<<final_line<<endl;
  }
  main_file.close();   /// close the file after reading is over.
  cout<<"point_line :"<<erasepoints<<endl;   ///that's why while loop is omitted here
 
  str_len=erasepoints.length();
   cout<<"the string length is:"<<str_len;
 

 int count = 0;

for (int i = 0; i < str_len; ++i)
if (erasepoints[i]==' ')
{
      ++count;
cout<<"count"<<count;
if(count=4)
string delete_string=erasepoints.substr(0,i);
string rest=erasepoints.substr(i);
cout<<"delete_string"<<delete_string;
cout<<"rest is:"<<rest;
cout<<i;
}
//int position=erasepoints.find(' ');
/*if(position=dim)
string sub=erasepoints.substr(0,position);*/
//cout<<"position"<<position;
/*if(count=dim)
{
      string sub=erasepoints.substr(0,2*dim-1);
      string rest=erasepoints.substr(2*dim);
      cout<<"\n"<<sub;
cout<<"\n"<<rest;*/

 return 0;
 }


i changed the code...then also its not working for me...
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20069606
As I said : you should really consider using {}'s around your loop and if blocks.

If you don't use them, then only one line will be considered as part of the block.

So, this :

        if(count=4)
        string delete_string=erasepoints.substr(0,i);
        string rest=erasepoints.substr(i);

Is the same as :

        if (count = 4) {
            string delete_string=erasepoints.substr(0,i);
        }
        string rest=erasepoints.substr(i);

which is probably not what you wanted.



A few more comments :

1) take care of the difference between = and ==. The first is an assignment, the second is a comparison.

        if (count = 4)

    is not the same as :

        if (count == 4)


2) if you create a variable, then it will be local to the enclosing block ... it will NOT be visible outside of the block. Keep that in mind.
    For example, if you do this :

        if (count == 4) {
            int value = 5;
        }
        // <--- we can NOT use value here !!!

    If you want value to be used outside of the if, then you need to do :

        int value = 0;
        if (count == 4) {
            value = 5;
        }
        // <--- we can use value here !!!


3) Try to properly indent your code : it will reveal a lot of problems, and will help you write better code with less errors !
0
 

Author Comment

by:trivenibomma
ID: 20069851
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <vector>
#include<ctime>
using namespace std;

int main ()
{
      string line, final_line, point_line, del_str,source,delete_string,rest,erasepoints;
      int str_len,dim;
 
      cout<< "Enter the points to be deleted from the file:";
      getline(cin,erasepoints);

      cout << "Enter the file name: ";
      cin>>source;
      const char* filename = source.c_str();

      cout<<"Enter the dimension:";
      cin>>dim;

      ifstream main_file(filename, ios::in);
      while (getline(main_file, line, '\n'))  /// read the file from start to end line by line into a STL string object
      {
            final_line += line;
            final_line += " \n";
            cout<<"final_line :"<<final_line<<endl;
      }
      main_file.close();   /// close the file after reading is over.
      cout<<"point_line :"<<erasepoints<<endl;   ///that's why while loop is omitted here
 
      str_len=erasepoints.length();
      cout<<"the string length is:"<<str_len;
 
      int count = 0;
      for (int i = 0; i < str_len; ++i)
      if (erasepoints[i]==' ')
      {
            ++count;
            cout<<"count"<<count;
            if(count==dim)
            {
                  string delete_string=erasepoints.substr(0,dim);
                  //string rest=erasepoints.substr(i);
                  int del_len=delete_string.length();
                  int del_pos=final_line.find(del_pos,del_len);
                  if(del_pos >= 0 )
                        final_line.erase(del_pos,del_len);  ////if found, erase the string.
                  else
                        cout<<point_line<<" is not found in main file."<<endl;
                  cout<<"final_line after :"<<final_line<<endl;
                  cout<<"\nrest is:"<<rest;
                  cout<<"\ni:"<<i;
            }
      }
      ofstream outfile(filename, ios::out);  ////now open the file to write the modified string into file and close it.
      outfile<<final_line;
      outfile.close();
       return 0;
}

i changed the indentation....but i think there is some thing wrong in the loops...
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 1000 total points
ID: 20070373
1) In this line :

                   string delete_string=erasepoints.substr(0,dim);

    dim is set to 4 ... Don't you want to use the loop index ?


2) Take a look at this line :

                  int del_pos=final_line.find(del_pos,del_len);

    What are you looking for ? And what are you really passing as parameter ? For reference :

        http://www.cplusplus.com/reference/string/string/find.html


3) Don't you want to remove all occurences of the delete_string ?


4) Don't you want to check for all parts of the erasepoints string ?
0
 

Author Comment

by:trivenibomma
ID: 20071362
i want to use the index....but i dont know how write the loop over there.....n i want to remove all the occurances of the delete_string from the file...
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20077701
>> i want to use the index....but i dont know how write the loop over there.....n i want to remove all the occurances of the delete_string from the file...

I know. The questions in my last post were rhetorical, to make you think about these issues you still have with your code.

And more specifically :

>> i want to use the index....

Then why do you use dim ?


>> but i dont know how write the loop over there.....

The easiest is to try to think about what has to happen exactly. ie. You need to find the start and finish of the substring you want to remove. Then you need to extract that substring, and search for it in the file. Try to follow the whole process, or better yet : work out an example on paper with all the details (indexes, lengths, etc.) - you'll get a much better idea of how the code works, and what you need to change.
It is vital that you understand what's happening, otherwise you'll keep guessing, and hoping you'll stumble on something that works. Programming just doesn't work that way : you need to understand and then implement exactly the way you want it.


>> n i want to remove all the occurances of the delete_string from the file...

Then you'll have to create a loop to remove them all. Ie. if you have erased one, find the next, and erase that one too, etc.




How far did you get in the last few days ? Did you get any further ?
0
 

Author Comment

by:trivenibomma
ID: 20079659
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <vector>
#include<ctime>
using namespace std;

void main()
{
      string erasepoints;
      int dim;

      cout<<"enter the erasepoints";
      getline(cin,erasepoints);
      cout<<"Enter the dimension";
      cin>>dim;
      int str_len=erasepoints.length();
      int count=0;
      for(int i=0;i<str_len;i++)
            if(erasepoints[i]=' ')
                  count++;
          if(count=dim)
            cout<<"i"<<i;
      

}

first i am trying to find out the position where the number of spaces will be equal to dimension...here the i m getting wrong output...can u tell me where i am doing wrong...
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 1000 total points
ID: 20079800
>>>> can u tell me where i am doing wrong...

The

     if(count=dim)

is wrong for more than one reason:

1. It needs to be within the block below the for loop. To define a block you need to use curly brackets:

    for(int i=0;i<str_len;i++)
    {
           ....    // all what is defined here now will be iterated
    }

2. if you want to compare two variables you have to use ==  (double =).

So it is

           if (count == dim)

3.  after you encountered the 'dim' you need to do what you intended to do

            if (count == dim)
            {
                    // now extract substring and make find and replacements
                    ....

                   // after that reset count so that you can get the next sequence
                   count = 0;
                   startpos = i+1;  // make the next position the start position for
                                             // the next sequence

            }


Note, startpos must be defined above the loop like the count variable. It's initial value is 0 and it is supposed to be the start position of a sequence within the string erasepoints.

0
 

Author Comment

by:trivenibomma
ID: 20079877
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <vector>
#include<ctime>
using namespace std;

void main()
{
      string erasepoints;
      int dim;

      cout<<"enter the erasepoints";
      getline(cin,erasepoints);
      cout<<"Enter the dimension";
      cin>>dim;
      int str_len=erasepoints.length();
      int count=0;
      for(int i=0;i<str_len;i++)
      {
            if(erasepoints[i]=' ')
                  count++;
          if(count==dim)
            {
                  cout<<"i"<<i;
                  string sub=erasepoints.substr(0,i);
                  string rest=erasepoints.substr(i);
                  count=0;
                  i=i+1;
            }
      }

}
i didnt able to find the position or the index where the 4th space character found...or dimension number of spaces are found...
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20080025
>>>> i didnt able to find the position or the index
>>>> where the 4th space character found...or
>>>> dimension number of spaces are found...

why not? what are your inputs? did you set a breakpoint to the cout statement? if not, you may not see the cout statement cause the console window already closed.

Instead of a breakpoint you may add

         int dummy;
         cin >> dummy;  

below the cout statement, simply to hold on if you detected the 4th space char.

>>>>     string sub=erasepoints.substr(0,i);

better use

           string sub=erasepoints.substr(startpos, i);

where startpos is defined above the for loop and initially was 0. That gives you the chance to continue the loop to find the next 4 numbers.

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20080192
Like me and Alex have said before : = is not the same as ==. Take a closer look at your code ;)
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20080460
>>>> if(erasepoints[i]=' ')
Take a closer look at your code ;)
0
 

Author Comment

by:trivenibomma
ID: 20088026
hey thanx for the help...

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <vector>
#include<ctime>
using namespace std;

int main ()
{
      string line, final_line, point_line, del_str,source,delete_string,rest,erasepoints;
      int str_len,dim;
 
      cout<< "Enter the points to be deleted from the file:";
      getline(cin,erasepoints);

      cout << "Enter the file name: ";
      cin>>source;
      const char* filename = source.c_str();

      cout<<"Enter the dimension:";
      cin>>dim;

      ifstream main_file(filename, ios::in);
      while (getline(main_file, line, '\n'))  /// read the file from start to end line by line into a STL string object
      {
            final_line += line;
            final_line += " \n";
            cout<<"final_line :"<<final_line<<endl;
      }
      main_file.close();   /// close the file after reading is over.
      cout<<"point_line :"<<erasepoints<<endl;   ///that's why while loop is omitted here
 
      str_len=erasepoints.length();
      cout<<"the string length is:"<<str_len;
 
      int count = 0;
      for (int i = 0; i < str_len; ++i)
      if (erasepoints[i]==' ')
      {
            ++count;
            cout<<"count"<<count;
            if(count==dim)
            {
                  string delete_string=erasepoints.substr(0,i);
                  string rest=erasepoints.substr(i+1);
                  int del_len=delete_string.length();
                  int del_pos=final_line.find(erasepoints,0);
                  if(del_pos >= 0 )
                        final_line.erase(del_pos,del_len);  ////if found, erase the string.
                  else
                        cout<<point_line<<" is not found in main file."<<endl;
                  cout<<"final_line after :"<<final_line<<endl;
                  cout<<"\nrest is:"<<rest;
                  cout<<"\ni:"<<i;
                  
            }
            
      }
      ofstream outfile(filename, ios::out);  ////now open the file to write the modified string into file and close it.
      outfile<<final_line;
      outfile.close();
       return 0;
}


but i didnt able to make it in the loop so that it can take all the points n divide according to the spaces...
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20088121
Three things :

1) right now, you only erase one occurrence of erase_string from final_line. What if it occurs more than once ?

2) at the end of the if(count==dim) block, you'll have to re-set the count variable, so it's ready for the next erase_string.

3) the substr(0,i) you use will only work for the first one, since you have 0 as the start point. You'll have to use a different (variable) start point.
0
 

Author Comment

by:trivenibomma
ID: 20088223
in the file it will occur only once...

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <vector>
#include<ctime>
using namespace std;

int main ()
{
      string line, final_line, point_line, del_str,source,delete_string,rest,erasepoints;
      int str_len,dim;
 
      cout<< "Enter the points to be deleted from the file:";
      getline(cin,erasepoints);

      cout << "Enter the file name: ";
      cin>>source;
      const char* filename = source.c_str();

      cout<<"Enter the dimension:";
      cin>>dim;

      ifstream main_file(filename, ios::in);
      while (getline(main_file, line, '\n'))  /// read the file from start to end line by line into a STL string object
      {
            final_line += line;
            final_line += " \n";
            cout<<"final_line :"<<final_line<<endl;
      }
      main_file.close();   /// close the file after reading is over.
      cout<<"point_line :"<<erasepoints<<endl;   ///that's why while loop is omitted here
 
      str_len=erasepoints.length();
      cout<<"the string length is:"<<str_len;
 
      int count = 0;
      int startpos=0;
      for (int i = 0; i < str_len; ++i)
      if (erasepoints[i]==' ')
      {
            ++count;
            cout<<"count"<<count;
            if(count==dim)
            {
                  string delete_string=erasepoints.substr(startpos,i);
                  //string rest=erasepoints.substr(i+1);
                  int del_len=delete_string.length();
                  int del_pos=final_line.find(erasepoints,0);
                  if(del_pos >= 0 )
                        final_line.erase(del_pos,del_len);  ////if found, erase the string.
                  else
                        cout<<point_line<<" is not found in main file."<<endl;
                  cout<<"final_line after :"<<final_line<<endl;
                  cout<<"\ndeletestring is:"<<delete_string;
                  cout<<"\ni:"<<i;
                  count=0;
                  startpos=i+1;
                  
            }
            
      }
      ofstream outfile(filename, ios::out);  ////now open the file to write the modified string into file and close it.
      outfile<<final_line;
      outfile.close();
       return 0;
}


yaa i changed the starting position for the substring...n i reset the count value

n its not  deleting the second substring...
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20088245
>> n its not  deleting the second substring...

Can you tell me what input you gave ? What's erasepoints set to ? And dim ?
0
 

Author Comment

by:trivenibomma
ID: 20088301
i m giving the erasepoints as 12 13 14 15 16 17 18 19 20

then first delete_string is:12 13 14 15

n it is not taking the second delete_string as 16 17 18 19

0
 

Author Comment

by:trivenibomma
ID: 20088307
n dim=4
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20088387
The second parameter of substr is the length, not the end index. Instead of i, you'll have to pass the length of the substring you want to extract. Try i - startpos

Also, the string you use in find should not be erasepoints ...
0
 

Author Comment

by:trivenibomma
ID: 20088484
hey thanx...i got it....
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20088495
Good ;)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question