Can you use seekp without ios::binary?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ishould
    New Member
    • Oct 2009
    • 4

    Can you use seekp without ios::binary?

    My actual problem is much larger but figuring out how to do this example will help me. I want to overwrite data that already exists in a text file but I can't seem to do this.

    ex.
    [file.txt]
    I have five fingers.

    My current code:

    Code:
    ofstream File;
    File.open("file.txt", ios::out);
    File.seekp(8, ios::beg);
    File << "four";
    File.close();
    I have tried different combinations of ios on the second line, but it either
    a) erases the whole file and writes "four" at the begining or end
    b) writes "four" at the very end ("I have five fingers.four")
    c) erases the whole file and writes "four" eight characters into the file

    I know I can get it to work by reading in the file and going through it word by word, but the actual file is extremely large so processing the whole file is not an option.
    Assuming I know the starting byte locations of everything I want to change, byte 8 in this case, how can I get the file.txt to display this:

    I have four fingers

    Is there any way to do this and avoid using ios::binary?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Well it looks like it should work to me. Have you check for erros after calling seekp? Have you tried using ofstream::write rather than <<?

    I know in C there was always trouble seeking in a text file because of the end of line mangling that was done by the underlying system.

    I am not sure if C++ has the same limitation but I would be inclined to suspect that it does in the absence of further evidence.

    Comment

    • ishould
      New Member
      • Oct 2009
      • 4

      #3
      when I change File << "four"; to File.write("fou r", 8); all that shows up in the file is "four" eight characters from the start of the file, everything else is gone

      Comment

      • ishould
        New Member
        • Oct 2009
        • 4

        #4
        *bump* Theres really no way to do this? Is my only option to convert the whole file to binary?

        Comment

        • ishould
          New Member
          • Oct 2009
          • 4

          #5
          Sorry for the triple post, but I figured it out so I thought I should post my findings for future reference. It seems weird but this works:

          ofstream File;
          File.open("file .txt", ios::in); //notice the ios::in, this is what made it work
          File.seekp(8, ios::beg);
          File << "four";
          File.close();

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Sorry it took a lot of diging most of my programming does not involve any reading or writing to disk.

            For ofstream ios::out implies ios:trunc. That is it is impossible to open using ofstream for writing without truncating.

            Your code works if you replace ofstream with fstream (bi-directional file stream).

            You may still have trouble seeking within a file in text mode because of the difference between the number of characters in the file and the number of characters apparently in the file if the end of line is represented by more than one character due to text mode mangling.

            If you do have to open in binary mode you don't have to convert your file, just be aware you will get "\r\n" on a windows system where you will get "\n" in text mode.

            Comment

            Working...