overwrite contents of existing file / manipulation ..

M

ma740988

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::eek: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::eek: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.
 
K

Kai-Uwe Bux

ma740988 said:
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::eek: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::eek: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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top