473,387 Members | 1,603 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.

How to edit a random position of a file with fstream apis?

Hi all,

I need to edit a random position of a file, for e.g, the 500th
byte.
To keep portability, I would like to use standard C++ instead of
os apis.
Could you help on this?
Thanks
Dec 4 '07 #1
3 2021
James Fang wrote:
I need to edit a random position of a file, for e.g, the 500th
byte.
To keep portability, I would like to use standard C++ instead of
os apis.
Could you help on this?
What exactly do you need help with? Open the file for read+write.
Position the stream at 500. Write the new byte. Close the stream.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 4 '07 #2
On Dec 4, 5:46 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
James Fang wrote:
I need to edit a random position of a file, for e.g, the 500th
byte.
To keep portability, I would like to use standard C++ instead of
os apis.
Could you help on this?
What exactly do you need help with? Open the file for read+write.
Position the stream at 500. Write the new byte. Close the stream.
Maybe he tried that and it didn't work.

There are several issues you have to pay attention to, at least
if you want to be portable:

-- First and foremost, the stream must be opened in binary
mode, and you must imbue the stream (or at least the
filebuf) with locale "C". Otherwise, a statement such as
"500th byte" doesn't really have any meaning.

-- The second thing is that if you really want to be strictly
portable, you have to use the two argument forms of seek,
e.g.: "myFile.seekp( 500, std::ios::beg )". In most
implementations, the one argument form will also work, but
according to the standard, there is in fact no guarantee
that an integral value will convert implicitly to a
streampos, and no guarantee that if it converts, the results
have any real relationship to the physical position in the
file.

And finally, as you say, you must open the file in read+write
mode (ios::in | ios::out), even if you're just writing; opening
in write only mode always truncates (unless you specify the
ios::app flag, but in that case, all writes are always to the
end of file, regardless of where you might be positionned
beforehand).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 5 '07 #3
222.210.236.214 - ËÄ´¨Ê¡³É¶¼ÊÐ µçÐÅ
"James Fang" <fa**********@gmail.comдÈëÏûÏ¢ÐÂÎÅ:a2************ **********************@e23g2000prf.googlegroups.co m...
Hi all,

I need to edit a random position of a file, for e.g, the 500th
byte.
To keep portability, I would like to use standard C++ instead of
os apis.
Could you help on this?
Thanks
Dec 13 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Oin Zea | last post by:
Is it possible for to program to access a random file at the same time and perform actions like create a new record?
16
by: Jason | last post by:
Hi, I need a way to use random numbers in c++. In my c++ project, when using the mingw compiler I used a mersenne twister that is publicly available and this did its job well. Now I have...
2
by: zhushenli | last post by:
Hello all, How to random read/write the file in fstream? BTW, how to access the file in block? Regards, Davy
8
by: Danny Smith | last post by:
Hi, I need to read a file and be able to: 1. Find the current position in the stream 2. Have access to a handy ReadLine() method. Obviously the FileStream class supports random access, so...
2
by: Senthil | last post by:
Hi, The need is to read each line in a text file and based on the read data decision has to be taken whether to reposition the file pointer. The StreamReader.Position does not give current...
4
by: Sanchit | last post by:
I want to know thta how can i edit a file in C++ For Example my file is Mr XyZ FFFFFF 65 And now i want go change this number 65 to 87.... how can i Do this..... I...
6
by: cooldisk | last post by:
Is it possible at all to read a binary file larger than 2GB on a 32- bit system? I tried the following: #include <iostream> #include <fstream> using namespace std; int main(int argc, char...
1
intOwnsVoid
by: intOwnsVoid | last post by:
This program compiles but it's not searching properly. Circle.h #ifndef CIRCLE_H #define CIRCLE_H #include <iostream> #include <fstream> using namespace std;
2
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. creating a method to search a tree to find the position of node and to return its pointer value basically i...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.