Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Editing a file without creating a temp file

Reply
Thread Tools

Editing a file without creating a temp file

 
 
Sooraj S
Guest
Posts: n/a
 
      01-19-2011
Hi,

I am new to cpp.

Is it possible to edit the content of a file without creating a temp
file.
ie I dont want to use this way.
1.using ifstream open the file in read mode
2.open a temp file using ofstream in write mode
3.read from the original file line by line and write to temp file.
4.write to temp file.

Can any one suggest any other way. Pls giev me a sample program to
File
-----
john 12345
kiran 4123
Alex 12344


I want to change kiran's number to 3333
 
Reply With Quote
 
 
 
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      01-19-2011
On Jan 19, 11:31*am, Sooraj S <(E-Mail Removed)> wrote:
> Hi,
>
> I am new to cpp.
>
> Is it possible to edit the content of a file without creating a temp
> file.
> ie I dont want to use this way.
> 1.using ifstream open the file in read mode
> 2.open a temp file using ofstream in write mode
> 3.read from the original file line by line and write to temp file.
> 4.write to temp file.


Yes, it is possible:
1. Open the file in read mode
2. Read the entire file in memory
3. close the file
4. Make whatever changes you want to the memory buffer
5. Open the file for writing
6. Write the memory buffer to the file
7. Close the file again.

Bart v Ingen Schenau
 
Reply With Quote
 
 
 
 
Dietmar Kuehl
Guest
Posts: n/a
 
      01-19-2011
On Jan 19, 10:31*am, Sooraj S <(E-Mail Removed)> wrote:
> Is it possible to edit the content of a file without creating a temp
> file.


You can open a file in read/write mode, read to the position where
you want to change things, do a seek to this position (e.g. using
an offset of 0 relative to the current position) and then write
the new data. Note, however, that this won't make any new space: it
just overwrites the existing bytes with new ones. Playing with
reading and writing a file only really works with fixed sized records.
The important bit about the actual code is that you need to have a
seek between switching from reading to writing or vise versa. Below
is sample code for this.

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <stdexcept>
#include <string>

int main(int ac, char* av[])
{
typedef std::istream_iterator<std::string> it;
typedef it::value_type str;
std::fstream file;
if (!(ac ^ 4) && (file.open(1[av]), file.is_open())
&& std::find(it(file), it(), str(2[av])) != it())
{
(file >> std::ws).seekg(0, std::ios_base::cur);
file << 3[av];
}
else
{
std::cerr << "ERROR: "
<< (!(ac ^ 4)
? "can't change "+str(2[av])+" of "+1[av]
: "usage: "+str(0[av])+" file name code")
<< "\n";
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 5.36 Why can't I use "C:\temp\foo" in DOS paths? Why doesn't `C:\temp\foo.exe` work? PerlFAQ Server Perl Misc 0 04-26-2011 10:00 AM
Modifying a file w/o creating a temp file Joe Hudson Java 3 05-24-2008 09:15 PM
How to initialize member reference with temp object and delete the temp object when the destructor is called? PengYu.UT@gmail.com C++ 2 05-09-2005 05:48 PM
File::Temp: opening the temp. file in "r+" mode? Also "man in the middle" A. Farber Perl Misc 3 03-03-2004 12:42 PM
Temp Internet and temp files now in winnt directory w2k pro Bibble Bobble Computer Support 1 11-27-2003 06:14 PM



Advertisments