473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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:

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?
Oct 15 '09 #1
5 3619
Banfa
9,065 Expert Mod 8TB
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.
Oct 15 '09 #2
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
Oct 15 '09 #3
*bump* Theres really no way to do this? Is my only option to convert the whole file to binary?
Oct 17 '09 #4
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();
Oct 17 '09 #5
Banfa
9,065 Expert Mod 8TB
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.
Oct 17 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: WahJava | last post by:
Hi developers, I'm running GCC (GNU C/C++ Compiler shipped with Fedora Core 2). I've written a program using IOStream classes, in which I'm reading files in ios::binary mode. I'm using read()...
103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
1
by: AlekseyUS | last post by:
Hi, I'm a little stuck, I basically need to copy all the information within a specific file in Temp and append it to a file in another location. I'm not having any problems with smaller size...
9
by: Ron Eggler | last post by:
Hi, I would like to write binary data in a file i open (ofstream) with ios::binary but it keeps failing and it gives me a segmentation fault. What I'm exactly doing: if (isBinary == true) {...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.