Thread: editing data in a data file

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    24

    editing data in a data file

    hi..is it possible clear data in a .dat file.

    lets say my file initially has:

    *******
    my name is sujeet.
    *******

    Now i want to clear all that data and have my file data:

    *******
    sujeet is my name.
    *******

    is it possible?wat function to use?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The same functions you used to write the file in the first place.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    24
    but how do i erase the data in the data file?

    waht command?

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    ofstream out ("somefile", std::ios::trunc);
    or to add to it but not delete
    ofstream out ("somefile", std::ios::app);

    just search google for a fstream reference.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    By data file you mean a .txt file? You need to #include <fstream> so you can read/write from files, atleast on a console program. There is no "erasing" of other data, you just write over it.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    scwizzo: I dont know if I misunderstood you, but <fstream> will read any file. Also, you can "erase" data from a file. Maybe not bits and pieces, but you can delete the whole thing, but then again if you can write it once, you can surely write the data to the file again.

    If I were the OP I would get on cppreference.com and learn about file I/O. I had my problems with it, as everyone else did once or twice. I think a good reference would be a great place to start.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  4. Editing a data file
    By Strait in forum C++ Programming
    Replies: 7
    Last Post: 02-05-2005, 04:21 PM
  5. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 11:47 AM