Thread: Heeeeelp please!!!!

  1. #1
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278

    Heeeeelp 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
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Your title is not what it should be according to the rules.

    But just because I am nice today I will give you a little hint how to do it:
    Read in all lines
    store them in a container of some sort
    Delete the line you want to be deleted
    Write to same file.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Better than that, there is a thread just like this one on the second page.

    It is always better to post in an existing thread than to start a new one.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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!!!!
    Oh, my god!!! Help!! Help!!!!!!!!!!!!! I can't breathe!!!!!! I just wanted to let you know I am reading your post!!! I decided I didn't have time to finishing reading it before I needed to respond because of the urgency of it all!!!

    You can't delete lines from a file!!!! But since you can read from a file and write to a file!!!! Oh, my god!!! Is there time??!!! It must be too late now!!!!
    Last edited by 7stud; 04-21-2006 at 09:28 AM.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You can't delete lines from a file, but under very special circumstances you can change any given "line" of a file. For this strategy to work the file has to be completely symetrical. That is, there must be a defined repeat in the memory blocks used by each "line" in the file. That is, in databases, one approach is to have each record stored in the database to consist of a series of fields. Each field can be of a specific type. Each type will have exactly the same memory requirements in each record. For types of fixed memory size such as int, or double, etc. that isn't a problem. But for types of variable memory size like strings, that imparts some restrictions that need to be followed precisely to get this strategy to work. So if the record had two types, say an int representing an id number, and a char array of 20 char that could store a name of x char, x being less than 20, and padding commas amounting to 20 - x commas per char array, then you could change either the id of a record or the name of the record as long as the amount of memory used by the "change" was exactly the same as the amount of memory used by the original. As you can see, this gets tricky fast, so if you can read everything into a container in memory and change what you want before writing back to file, go for it!
    You're only born perfect.

  6. #6
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    Quote Originally Posted by anirban
    I have posted it earlier but i did not get any sure answer.
    Yes you did. In your earlier thread, ZuK told you exactly what to do. Now Zuk, Shakti, and elad have all told you precisely what you need to know, and, on top of that, citizen and I have pointed out that other people have told you what to do (helpful, aren't we?). Clearly, such a cornucopia of help - a veritable buffet of sound advice - should get you on your way. In addition to this, I'd add a tidbit: If you want to avoid having your threads locked, don't SCREAM!! FOR!! HELP!! PRETTY PLEASE WITH SUGAR!!! AND SPRINKLES!!!!! ON TOP!!!! and don't ignore what people have already told you. If you don't understand something about their answer, ask for clarification - don't just keep asking the same question over and over again. It's quite pointless.
    There is a difference between tedious and difficult.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Read in all lines
    store them in a container of some sort
    Delete the line you want to be deleted
    Write to same file.
    There was a thread on this topic in the C forum, and it mentions why not to write back again to the same file (Salem mentioned it).

    Also see http://board.theprogrammingsite.com/viewtopic.php?t=115
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed