Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > overwrite contents of existing file / manipulation ..

Reply
Thread Tools

overwrite contents of existing file / manipulation ..

 
 
ma740988
Guest
Posts: n/a
 
      03-05-2006
Trying to some of my reading on file manipulation
(ifstream/ofstream/fstream) into action. So now consider.

# include <iostream>
# include <fstream>
# include <cstring>
# include <sstream>

bool remove_and_create_if_exists(
const std::string& filename
)
{
if (std::remove(filename.c_str()) != 0)
return false;
std:fstream f(filename.c_str());
if (!f) return false; return true;
}


int main()
{
int const kibibyte = 0x400;
int const mebibyte = kibibyte * kibibyte;
std::string const file_name("outfile.txt");

std::ifstream in( file_name.c_str() );
std:fstream out;

if ( !in )
{
out.open( file_name.c_str() ); // if file doesn't exist create it
}
else {
out.clear();
out << in.rdbuf(); // now overwrite the 'output file with the
// the file found .. i.e. copy the file
in.close();
}
int offset = mebibyte;
for ( int idx(0); idx < 10; ++idx )
{
out << idx << ' ' << std::hex
<< "0x" << offset << endl;
offset += mebibyte;
}
return 0;
}


I check for the 'existence' of the file. If file exists, I copy the
contents of the input file to the output file, which brings about my
dilema.

Lets assume the outfile.txt _exists_ (key) - on your drive - and it's
contents is follows:

0 100000
1 200000
2 300000
3 400000
4 500000
5 600000
6 700000
7 800000
8 900000
9 a00000

If you execute the program. outfile.txt should be overwritten to look
like:
0 0x100000
1 0x200000
2 0x300000
3 0x400000
4 0x500000
5 0x600000
6 0x700000
7 0x800000
8 0x900000
9 0xa00000

That's not happening and I'm unsure why?. Secondly, I'd like to
believe I could get away with one file stream object to read and write
to the file. As opposed to an ifstream/ofstream object. I tried using
fstream (as opposed to ifstream/ofstream) but came up short. Ideas?


An aside: In the end here's what I'm opting to do:. I'd like to
create a menu driven/_interactive_ program (cin) .

At program invocation, I'll open the file. If file exists keep
existing, if not create. No different than what I'm trying to do do
above.

The user is then given a list of menu options.
1. The user will provide a list of 'offsets' which I'll append to
the existing offsets within the file.
2. The user has the option to tell me to backup 'X' spaces within
the file. This could be - start from the beginning or back up two
'offsets'. For instance. Assume outfile.txt had:

0 0x100000
1 0x200000
2 0x300000
If the user desired for me to back up 2 'offsets'. On my next write,
I'll start by overwriting 1 0x20000.

When that's done, I'll try incorporating the code in a class. When
that's done, I'll try using some of that iterator 'stuff'
(istreambuf_iterator business) to .... I'm not sure what yet..
When that's done, .. we'll I ran out of ideas

As always, thanks in advance.

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      03-06-2006
ma740988 wrote:

> Trying to some of my reading on file manipulation
> (ifstream/ofstream/fstream) into action. So now consider.
>
> # include <iostream>
> # include <fstream>
> # include <cstring>
> # include <sstream>
>
> bool remove_and_create_if_exists(
> const std::string& filename
> )
> {
> if (std::remove(filename.c_str()) != 0)
> return false;
> std:fstream f(filename.c_str());
> if (!f) return false; return true;
> }
>
>
> int main()
> {
> int const kibibyte = 0x400;
> int const mebibyte = kibibyte * kibibyte;
> std::string const file_name("outfile.txt");
>
> std::ifstream in( file_name.c_str() );
> std:fstream out;
>
> if ( !in )
> {
> out.open( file_name.c_str() ); // if file doesn't exist create it
> }
> else {


In this branch, out is never opened. I wonder, how the stream would know
which file it is supposed to connect to.

> out.clear();
> out << in.rdbuf(); // now overwrite the 'output file with the
> // the file found .. i.e. copy the file
> in.close();
> }
> int offset = mebibyte;
> for ( int idx(0); idx < 10; ++idx )
> {
> out << idx << ' ' << std::hex
> << "0x" << offset << endl;
> offset += mebibyte;
> }
> return 0;
> }
>
>
> I check for the 'existence' of the file. If file exists, I copy the
> contents of the input file to the output file, which brings about my
> dilema.


Are you sure that is what the code says? You send the contents of the file
to a stream that was never opened.


> Lets assume the outfile.txt _exists_ (key) - on your drive - and it's
> contents is follows:
>
> 0 100000
> 1 200000
> 2 300000
> 3 400000
> 4 500000
> 5 600000
> 6 700000
> 7 800000
> 8 900000
> 9 a00000
>
> If you execute the program. outfile.txt should be overwritten to look
> like:
> 0 0x100000
> 1 0x200000
> 2 0x300000
> 3 0x400000
> 4 0x500000
> 5 0x600000
> 6 0x700000
> 7 0x800000
> 8 0x900000
> 9 0xa00000
>
> That's not happening and I'm unsure why?.


See above.

> Secondly, I'd like to
> believe I could get away with one file stream object to read and write
> to the file. As opposed to an ifstream/ofstream object. I tried using
> fstream (as opposed to ifstream/ofstream) but came up short. Ideas?


What about openening the file to append?

[snip}


Best

Kai-Uwe Bux
 
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
How do I overwrite existing web.config with a new one? Andy ASP .Net 5 06-12-2007 05:26 PM
Mistakenly Overwrite Word File - Can the original file be restored =?Utf-8?B?ZWlk?= Microsoft Certification 4 08-30-2006 04:59 PM
overwrite existing value of a textbox with null shruti tiwari via JavaKB.com Java 1 04-03-2005 04:24 AM
Re: file overwrite data manipulation John Harrison C++ 2 06-27-2003 05:12 AM
Re: file overwrite data manipulation Chris Theis C++ 0 06-26-2003 07:32 AM



Advertisments