file writing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bob@coolgroups.com

    #1

    file writing

    Is there a way to overwrite a random byte in a file using FILE without
    rewriting the whole file?

  • Victor Bazarov

    #2
    Re: file writing

    bob@coolgroups. com wrote:[color=blue]
    > Is there a way to overwrite a random byte in a file using FILE without
    > rewriting the whole file?[/color]

    Yes. Open the file for reading/writing, then use fseek to get to the
    position where you want to write, then use fwrite, then close it. IIRC
    on some platforms you could end up with a truncated file unless you
    open it for both reading and writing, you will need to check with your
    documentation.

    V


    Comment

    • Kern

      #3
      Re: file writing

      But it's quite classic way to moditfy the binary file.
      How can we do this in a modern cpp way?

      Comment

      • bob@coolgroups.com

        #4
        Re: file writing

        is it fopen("file", "r+b");

        ???

        Comment

        • Tribal-Tec

          #5
          Re: file writing

          The modern cpp way would be the use of fstream;

          std::ofstream myFile; // or fstream for both writing and reading
          myFile.open( "filename", std::ios_base:: binary | std::ios_base:: out );

          now with seekg method you can place the write cursor to modify your
          desired byte.

          Regards

          Comment

          • BobR

            #6
            Re: file writing


            Tribal-Tec wrote in message
            <1134216580.335 287.171610@g44g 2000cwa.googleg roups.com>...[color=blue]
            >The modern cpp way would be the use of fstream;
            >std::ofstrea m myFile; // or fstream for both writing and reading
            >myFile.open( "filename", std::ios_base:: binary | std::ios_base:: out );
            >
            >now with seekg method you can place the write cursor to modify your
            >desired byte.[/color]

            The 'g' in 'seekg' means 'get' to me. Use 'seekp' to 'put'.

            std::ofstream MyFile("MyFile. txt", std::ios_base:: binary );
            if(!MyFile){ std::cout<<"\n ofstream FAILED"<<std::e ndl; /*exit(1);*/}
            std::cout<<"ofs tream MyFile.tellp() = "<<MyFile.tellp ()<<std::endl;
            MyFile.seekp(10 , std::ios::end);
            std::cout<<"MyF ile.seekp(10, end) MyFile.tellp() ="
            <<MyFile.tellp( )<<std::endl;
            MyFile.seekp(10 , std::ios::beg);
            std::cout<<"MyF ile.seekp(10, beg) MyFile.tellp() ="
            <<MyFile.tellp( )<<std::endl;
            MyFile.close();

            --
            Bob R
            POVrookie


            Comment

            • Chris Theis

              #7
              Re: file writing


              "Kern" <kernel1983@gma il.com> wrote in message
              news:1134189993 .468429.27470@o 13g2000cwo.goog legroups.com...[color=blue]
              > But it's quite classic way to moditfy the binary file.
              > How can we do this in a modern cpp way?
              >[/color]

              Surely you could use streams but what will you gain in this case?

              Cheers
              Chris


              Comment

              Working...