overwrite contents of existing file / manipulation ..

Discussion in 'C++' started by ma740988, Mar 5, 2006.

  1. ma740988

    ma740988 Guest

    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.
     
    ma740988, Mar 5, 2006
    #1
    1. Advertising

  2. ma740988

    Kai-Uwe Bux Guest

    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::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
     
    Kai-Uwe Bux, Mar 6, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shruti tiwari via JavaKB.com

    overwrite existing value of a textbox with null

    shruti tiwari via JavaKB.com, Apr 2, 2005, in forum: Java
    Replies:
    1
    Views:
    668
    David.Federman
    Apr 3, 2005
  2. John Harrison

    Re: file overwrite data manipulation

    John Harrison, Jun 26, 2003, in forum: C++
    Replies:
    2
    Views:
    5,963
    John Harrison
    Jun 27, 2003
  3. Chris Theis
    Replies:
    0
    Views:
    812
    Chris Theis
    Jun 26, 2003
  4. Andy
    Replies:
    5
    Views:
    576
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Jun 12, 2007
  5. Kamarulnizam Rahim
    Replies:
    4
    Views:
    226
    Robert Klemme
    Jan 28, 2011
Loading...

Share This Page