473,414 Members | 1,888 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,414 software developers and data experts.

file writing

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

Dec 10 '05 #1
6 1216
bo*@coolgroups.com wrote:
Is there a way to overwrite a random byte in a file using FILE without
rewriting the whole file?


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
Dec 10 '05 #2
But it's quite classic way to moditfy the binary file.
How can we do this in a modern cpp way?

Dec 10 '05 #3
bob
is it fopen("file", "r+b");

???

Dec 10 '05 #4
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

Dec 10 '05 #5

Tribal-Tec wrote in message
<11**********************@g44g2000cwa.googlegroups .com>...
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.


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::endl; /*exit(1);*/}
std::cout<<"ofstream MyFile.tellp() = "<<MyFile.tellp()<<std::endl;
MyFile.seekp(10, std::ios::end);
std::cout<<"MyFile.seekp(10, end) MyFile.tellp() ="
<<MyFile.tellp()<<std::endl;
MyFile.seekp(10, std::ios::beg);
std::cout<<"MyFile.seekp(10, beg) MyFile.tellp() ="
<<MyFile.tellp()<<std::endl;
MyFile.close();

--
Bob R
POVrookie
Dec 10 '05 #6

"Kern" <ke********@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
But it's quite classic way to moditfy the binary file.
How can we do this in a modern cpp way?


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

Cheers
Chris
Dec 10 '05 #7

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

Similar topics

48
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential...
2
by: Pierre Rouleau | last post by:
Hi all! I'm trying to extend the functionality of the file object by creating a class that derives from file. MyFile class re-implements __init__(), write(), writelines() and close() to augment...
8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
8
by: Stewart | last post by:
is there any way this can be done? I've looked at the help files and checked out as many tutorials as i could find on the net (as always) but no joy. thanks
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
3
by: StGo | last post by:
How can i read/write file's custom attributs(like subject,author...) in C#??? Thanks :))
4
by: Doug | last post by:
Hi, It looks like the only way to get a size of a file within csharp is to use FileInfo and the Length property. However that only returns the number of bytes in the file which is translating...
14
by: prasadjoshi124 | last post by:
Hi All, I am writing a small tool which is supposed to fill the filesystem to a specified percent. For, that I need to read how much the file system is full in percent, like the output given...
3
by: brook | last post by:
hey all - i´m new to php and having trouble writing a simple code which should create a file. here is the most simplified version: <?php $content = "my content"; $path = "test.txt";...
12
by: glennanthonyb | last post by:
Hi The company I work for has finally woken up to data security on our field laptops. I'm writing something in C# that will allow remote deletion of sensitive data and I don't believe...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.