Connecting Tech Pros Worldwide Help | Site Map

Can you use seekp without ios::binary?

Newbie
 
Join Date: Oct 2009
Posts: 4
#1: Oct 15 '09
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:

Expand|Select|Wrap|Line Numbers
  1. ofstream File;
  2. File.open("file.txt", ios::out);
  3. File.seekp(8, ios::beg);
  4. File << "four";
  5. File.close();
  6.  
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's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,148
#2: Oct 15 '09

re: Can you use seekp without ios::binary?


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.
Newbie
 
Join Date: Oct 2009
Posts: 4
#3: Oct 15 '09

re: Can you use seekp without ios::binary?


when I change File << "four"; to File.write("four", 8); all that shows up in the file is "four" eight characters from the start of the file, everything else is gone
Newbie
 
Join Date: Oct 2009
Posts: 4
#4: Oct 17 '09

re: Can you use seekp without ios::binary?


*bump* Theres really no way to do this? Is my only option to convert the whole file to binary?
Newbie
 
Join Date: Oct 2009
Posts: 4
#5: Oct 18 '09

re: Can you use seekp without ios::binary?


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();
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,148
#6: Oct 18 '09

re: Can you use seekp without ios::binary?


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.
Reply

Tags
binary, ios, replace, seekp


Similar C / C++ bytes