If I want to delete specified data in a saved file, without deleting the whole data in that file. how can I do that?
nadia nadia 86
maha maha 203

if i want to delete "nadia nadia 86" only
how can I do it?

This is not just a C++ problem. It is a problem in the way files and file streams in general work.

You do not directly. A stream of data is just that - a stream. You write it or read it sequentially. File streams however do usually have random access allowing you to position for reading or writing within a file (see the tellg, seekg (read positioning) and tellp, seekp (write positioning) member functions) - which I suppose you could use but this would be complicated as you would have to read through the file and find the start of the text to be removed, then you would have to read all the data after this text and write to the previously located deleted text start position. The final problem would be to truncate the stream so as to remove the extraneous data at the end.

An easier way would be to use two streams - one to read the existing file and one to write the updated file. This is safer if something goes wrong as you leave the existing file as is until the very last moment, when it can be switched for the updated copy.

You read the existing file, in this case I would suggest you do it line by line. Each line is checked to see if it should be deleted. If not then it is written to the output stream. If it is to be deleted then it is not written and you move on to reading the next line.

At the end the output file contains the updated data and the original file is unchanged. You can then rename the original file to some backup-filename (e.g. making the last character a ~) and then rename the new file to be the same name as the original file (close the streams first though!). You can then delete the original file if you wish. You can do this either from the operating system or from your program using relevant functions (e.g. the C library rename function and the UN*X function unlink - _unlink in the MSVC++ runtime library).  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.


©2017 About.com. All rights reserved.

[an error occurred while processing this directive]