Replace write in fstream

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Slaughter

    #1

    Replace write in fstream

    I'm trying to replace some bytes in a file using fstream but all I seem to
    be able to do is append or 0 the file then write... Basicaly I just need to
    replace the first 512 bytes of the file with some buffer(but eventually I
    will need to replace random blocks in the file at different locations). Is
    there anyway to do this efficiently using fstream? (I know I can duplicate
    the file, but it seems a waste to rewrite the same data for no reason).

    Thanks.


  • Jon Slaughter

    #2
    Re: Replace write in fstream


    "Jon Slaughter" <nobody@nowhere .com> wrote in message
    news:10t4c6s9hl sts51@corp.supe rnews.com...[color=blue]
    > I'm trying to replace some bytes in a file using fstream but all I seem to
    > be able to do is append or 0 the file then write... Basicaly I just need
    > to replace the first 512 bytes of the file with some buffer(but eventually
    > I will need to replace random blocks in the file at different locations).
    > Is there anyway to do this efficiently using fstream? (I know I can
    > duplicate the file, but it seems a waste to rewrite the same data for no
    > reason).
    >
    > Thanks.
    >[/color]

    my code:

    fstream VM_Drive(argv[2], ios::out | ios::binary);

    VM_Drive.seekp( 0, ios_base::beg);

    char buf[512];

    for(int i = 0; i<512;i++) buf[i] = 2;

    VM_Drive.write( buf, 512);

    VM_Drive.close( );



    I've tried ios:app which will append the data, but won't replace.



    the ios::out seems to be working exactly as ios::trunc. It resizes the file
    to 0 when I open.


    Comment

    • Jonathan Turkanis

      #3
      Re: Replace write in fstream

      Jon Slaughter wrote:[color=blue]
      > "Jon Slaughter" <nobody@nowhere .com> wrote in message
      > news:10t4c6s9hl sts51@corp.supe rnews.com...[color=green]
      >> I'm trying to replace some bytes in a file using fstream but all I
      >> seem to be able to do is append or 0 the file then write... Basicaly
      >> I just need to replace the first 512 bytes of the file with some
      >> buffer(but eventually I will need to replace random blocks in the
      >> file at different locations). Is there anyway to do this efficiently
      >> using fstream? (I know I can duplicate the file, but it seems a
      >> waste to rewrite the same data for no reason).
      >>
      >> Thanks.
      >>[/color]
      >
      > my code:
      >
      > fstream VM_Drive(argv[2], ios::out | ios::binary);[/color]

      try ios::in | ios::out | ios::binary

      Jonathan


      Comment

      • Evan Carew

        #4
        Re: Replace write in fstream

        -----BEGIN PGP SIGNED MESSAGE-----
        Hash: SHA1

        Jon,

        Jon Slaughter wrote:[color=blue]
        > "Jon Slaughter" <nobody@nowhere .com> wrote in message
        > news:10t4c6s9hl sts51@corp.supe rnews.com...
        >[color=green]
        >>I'm trying to replace some bytes in a file using fstream but all I seem to
        >>be able to do is append or 0 the file then write... Basicaly I just need
        >>to replace the first 512 bytes of the file with some buffer(but eventually
        >>I will need to replace random blocks in the file at different locations).
        >>Is there anyway to do this efficiently using fstream? (I know I can
        >>duplicate the file, but it seems a waste to rewrite the same data for no
        >>reason).
        >>
        >>Thanks.
        >>[/color]
        >
        >
        > my code:
        >
        > fstream VM_Drive(argv[2], ios::out | ios::binary);[/color]
        This is fine as far as it goes, however, if you have a look at
        streambuf.h in your STL, you will see a flag called nocreate, and
        another one called noreplace. Both of these do about what you would
        expect. As for the binary flag, this is only needed on those platforms
        which make a distinction between the two types of files (i.e. on MS
        windows).[color=blue]
        >
        > VM_Drive.seekp( 0, ios_base::beg);
        >
        > char buf[512];
        >
        > for(int i = 0; i<512;i++) buf[i] = 2;
        >
        > VM_Drive.write( buf, 512);
        >
        > VM_Drive.close( );
        >[snip][/color]

        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v1.0.6 (GNU/Linux)
        Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

        iD8DBQFB0rwupxC QXwV2bJARAhmYAJ 9h68lCfdSPEENCt tHL1JMeXe7MAwCZ AeVr
        kCZ5Xn7M2F0hyFO wLsfYEms=
        =zimQ
        -----END PGP SIGNATURE-----

        Comment

        • Jon Slaughter

          #5
          Re: Replace write in fstream


          "Jonathan Turkanis" <technews@kanga roologic.com> wrote in message
          news:33erg8F3tk ns3U1@individua l.net...[color=blue]
          > Jon Slaughter wrote:[color=green]
          >> "Jon Slaughter" <nobody@nowhere .com> wrote in message
          >> news:10t4c6s9hl sts51@corp.supe rnews.com...[color=darkred]
          >>> I'm trying to replace some bytes in a file using fstream but all I
          >>> seem to be able to do is append or 0 the file then write... Basicaly
          >>> I just need to replace the first 512 bytes of the file with some
          >>> buffer(but eventually I will need to replace random blocks in the
          >>> file at different locations). Is there anyway to do this efficiently
          >>> using fstream? (I know I can duplicate the file, but it seems a
          >>> waste to rewrite the same data for no reason).
          >>>
          >>> Thanks.
          >>>[/color]
          >>
          >> my code:
          >>
          >> fstream VM_Drive(argv[2], ios::out | ios::binary);[/color]
          >
          > try ios::in | ios::out | ios::binary
          >
          > Jonathan
          >
          >[/color]

          that doesn't work... it still recreates the file.


          Comment

          • Jon Slaughter

            #6
            Re: Replace write in fstream


            "Evan Carew" <tempcarew@pobo x.com> wrote in message
            news:10t5f1gftt 42787@corp.supe rnews.com...[color=blue]
            > -----BEGIN PGP SIGNED MESSAGE-----
            > Hash: SHA1
            >
            > Jon,
            >
            > Jon Slaughter wrote:[color=green]
            >> "Jon Slaughter" <nobody@nowhere .com> wrote in message
            >> news:10t4c6s9hl sts51@corp.supe rnews.com...
            >>[color=darkred]
            >>>I'm trying to replace some bytes in a file using fstream but all I seem
            >>>to be able to do is append or 0 the file then write... Basicaly I just
            >>>need to replace the first 512 bytes of the file with some buffer(but
            >>>eventually I will need to replace random blocks in the file at different
            >>>locations) . Is there anyway to do this efficiently using fstream? (I know
            >>>I can duplicate the file, but it seems a waste to rewrite the same data
            >>>for no reason).
            >>>
            >>>Thanks.
            >>>[/color]
            >>
            >>
            >> my code:
            >>
            >> fstream VM_Drive(argv[2], ios::out | ios::binary);[/color]
            > This is fine as far as it goes, however, if you have a look at streambuf.h
            > in your STL, you will see a flag called nocreate, and another one called
            > noreplace. Both of these do about what you would expect. As for the binary
            > flag, this is only needed on those platforms which make a distinction
            > between the two types of files (i.e. on MS windows).[/color]


            I don't understand how these flags are useful? I am trying to open a
            pre-existing file and overwrite only part of it. But my code truncates the
            file for no reason. (if the file is 1024 bytes and I write 512 bytes, the
            "new" file is 512 bytes... and I don't want to recreate the file because it
            can be up to several gigs). I'm not sure whats going on, but it seems
            ios::out is acting just like ios::trunc.

            (and yes, I'm using windows)
            [color=blue][color=green]
            >>
            >> VM_Drive.seekp( 0, ios_base::beg);
            >>
            >> char buf[512];
            >>
            >> for(int i = 0; i<512;i++) buf[i] = 2;
            >>
            >> VM_Drive.write( buf, 512);
            >>
            >> VM_Drive.close( );
            >>[snip][/color]
            >
            > -----BEGIN PGP SIGNATURE-----
            > Version: GnuPG v1.0.6 (GNU/Linux)
            > Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
            >
            > iD8DBQFB0rwupxC QXwV2bJARAhmYAJ 9h68lCfdSPEENCt tHL1JMeXe7MAwCZ AeVr
            > kCZ5Xn7M2F0hyFO wLsfYEms=
            > =zimQ
            > -----END PGP SIGNATURE-----[/color]


            Comment

            • Jonathan Mcdougall

              #7
              Re: Replace write in fstream

              Evan Carew wrote:[color=blue]
              > Jon,
              >
              > Jon Slaughter wrote:[color=green][color=darkred]
              >>>I'm trying to replace some bytes in a file using fstream but all I seem to
              >>>be able to do is append or 0 the file then write... Basicaly I just need
              >>>to replace the first 512 bytes of the file with some buffer(but eventually
              >>>I will need to replace random blocks in the file at different locations).
              >>>Is there anyway to do this efficiently using fstream? (I know I can
              >>>duplicate the file, but it seems a waste to rewrite the same data for no
              >>>reason).[/color]
              >>
              >>my code:
              >>
              >>fstream VM_Drive(argv[2], ios::out | ios::binary);[/color]
              >
              > This is fine as far as it goes, however, if you have a look at
              > streambuf.h in your STL, you will see a flag called nocreate, and
              > another one called noreplace. Both of these do about what you would
              > expect. As for the binary flag, this is only needed on those platforms
              > which make a distinction between the two types of files (i.e. on MS
              > windows).[/color]

              nocreate and noreplace are non standard flags,
              prefer not to use them if you can.


              Jonathan

              Comment

              Working...