delete a line from a file [was:Heeeeeelp please!!!!]


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: delete a line from a file [was:Heeeeeelp please!!!!]

Hybrid View

  1. #1
    Join Date
    Apr 2006
    Posts
    1

    delete a line from a file [was:Heeeeeelp please!!!!]

    How can i delete a line from a file created using a C++ program?Can i get a code sample please? I am in desperate need of it!!!
    I need it because i am writing a dbms project where i need to delete a line from a dat file. I have done the following but its not working!!I have posted it earlier but i did not get any sure answer. Please help!!!!


    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
        ofstream fout("file.dat",ios::out);
        fout << "Sunanda 5 \n";
        fout << "Rahool 10 \n";
        fout << "Sunetra 12 \n";
        fout.close();
        
        ofstream fapp("file.dat",ios::out);
        fapp.seekp(23);
        int i = 0;
        while(i < 11)
        {
                fapp << '\b';
                i++;
        }        
        fapp.close();
        system("pause");                   
    }

  2. #2
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Hi,

    I don't think you can do that. I suggest you read in your file and write everything but the line you want to delete to a temporary file and then rename the temporary file to your original file. It's not very efficient, but it'll work. Alternatively you can open your file as read/write and overwrite the line you want to delete with the rest of the file...

    Cheers,

    Dieter

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    In standard C++ it's quite diffciult to do this because you will need to isntrcut the file syetm that the file's size has changed. A simpler solution is to read teh file into a memory buffer, manipulate it, and then rewrite the whole buffer into the original file, overwriting the previous content.
    Danny Kalev

Similar Threads

  1. Problem compiling a C++ file in Linux
    By kepler in forum C++
    Replies: 3
    Last Post: 08-26-2007, 12:11 AM
  2. wav file fade in out help
    By jase_dukerider in forum C++
    Replies: 2
    Last Post: 04-14-2005, 07:48 PM
  3. NullPointerException when reading text file
    By Andrew McLellan in forum Java
    Replies: 3
    Last Post: 05-09-2001, 05:34 PM
  4. Replies: 0
    Last Post: 03-02-2001, 01:31 AM
  5. Replies: 1
    Last Post: 05-03-2000, 11:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center