473,795 Members | 3,279 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

File Seeking / Overwriting bytes

Hi,

I need to open an existing file, seek to a position at X number of
bytes, and write out Y number of bytes overwriting any existing bytes,
but no erasing any other data. Is this possible?

I've opened a file in append "a" mode, and then used fseek with
SEEK_SET to seek to 0 bytes into the file, but this sets the position
at the end of existing data, not actually at the start of the file.

So say I have a file of 8 bytes: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
0xFF
I want to open it, seek to a specific offset, lets say 2 bytes in,
then write out 4 bytes with the value 1D overwriting existing bytes,
leaving the file with: 0xFF 0xFF 0x1D 0x1D 0x1D 0x1D 0xFF 0xFF

Heres my testing code.

FILE *cont;
cont = fopen("/home/jk/test.txt", "a");
fseek(cont, 0, SEEK_SET);
fputs("T", cont);
fclose(cont);
Please can someone point me in the right direction, I want to avoid
reading everything in, editing it and rewriting it all out because the
file could be huge.

Many thanks,
Jason
Nov 13 '08 #1
27 7550
Jason <je******@gmail .comwrites:
Hi,

I need to open an existing file, seek to a position at X number of
bytes, and write out Y number of bytes overwriting any existing bytes,
but no erasing any other data. Is this possible?

I've opened a file in append "a" mode, and then used fseek with
SEEK_SET to seek to 0 bytes into the file, but this sets the position
at the end of existing data, not actually at the start of the file.
You want the "r+" mode.
Nov 13 '08 #2
On 13 Nov, 21:39, Nate Eldredge <n...@vulcan.la nwrote:
Jason <jeche...@gmail .comwrites:
Hi,
I need to open an existing file, seek to a position at X number of
bytes, and write out Y number of bytes overwriting any existing bytes,
but no erasing any other data. Is this possible?
I've opened a file in append "a" mode, and then used fseek with
SEEK_SET to seek to 0 bytes into the file, but this sets the position
at the end of existing data, not actually at the start of the file.

You want the "r+" mode.
Ah that simple, thanks very much works perfectly.
Nov 13 '08 #3
Jason wrote:
Hi,

I need to open an existing file, seek to a position at X number of
bytes, and write out Y number of bytes overwriting any existing bytes,
but no erasing any other data. Is this possible?
Yes, for suitable file types (that is, for files that make
sense when accessed via "binary" rather than "text" streams).
I've opened a file in append "a" mode, and then used fseek with
SEEK_SET to seek to 0 bytes into the file, but this sets the position
at the end of existing data, not actually at the start of the file.
"Append" means "all new data goes at the end." If you want to
write data at some other spot, use "rb+" or "r+b", both of which
mean "open an existing file for reading (r) and update (+), in
binary mode (b)."
So say I have a file of 8 bytes: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
0xFF
I want to open it, seek to a specific offset, lets say 2 bytes in,
then write out 4 bytes with the value 1D overwriting existing bytes,
leaving the file with: 0xFF 0xFF 0x1D 0x1D 0x1D 0x1D 0xFF 0xFF

Heres my testing code.

FILE *cont;
cont = fopen("/home/jk/test.txt", "a");
Use "rb+", as noted above. Also, in a real program you should
check whether I/O operations succeed or fail, especially the fopen()
calls because they are particularly susceptible to failure.
fseek(cont, 0, SEEK_SET);
"2 bytes in," I think you said? Then why the zero? This seek
positions the stream at the very beginning of the file, not at at
the third byte.
fputs("T", cont);
This writes the single byte 'T', not the desired four bytes
of '\x1d'.
fclose(cont);
Please can someone point me in the right direction, I want to avoid
reading everything in, editing it and rewriting it all out because the
file could be huge.
--
Er*********@sun .com
Nov 13 '08 #4
Jason wrote:
Hi,

I need to open an existing file, seek to a position at X number of
bytes, and write out Y number of bytes overwriting any existing bytes,
but no erasing any other data. Is this possible?

I've opened a file in append "a" mode, and then used fseek with
SEEK_SET to seek to 0 bytes into the file, but this sets the position
at the end of existing data, not actually at the start of the file.

So say I have a file of 8 bytes: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
0xFF
I want to open it, seek to a specific offset, lets say 2 bytes in,
then write out 4 bytes with the value 1D overwriting existing bytes,
leaving the file with: 0xFF 0xFF 0x1D 0x1D 0x1D 0x1D 0xFF 0xFF

Heres my testing code.

FILE *cont;
cont = fopen("/home/jk/test.txt", "a");
fseek(cont, 0, SEEK_SET);
fputs("T", cont);
fclose(cont);
Please can someone point me in the right direction, I want to avoid
reading everything in, editing it and rewriting it all out because the
file could be huge.

Many thanks,
Jason
Try this..

#include <stdio.h>
#include <stdlib.h>

int main(void) {
FILE *fp;
char *file = "jc.txt";
int c;

fp = fopen(file, "w");
fprintf(fp, "My name is jason.\n");
fclose(fp);

fp = fopen(file, "r");
while ((c = getc(fp)) != EOF) putchar(c);
fclose(fp);

fp = fopen(file, "r+");
fseek(fp, 11, SEEK_SET);
fputc('J', fp);
fclose(fp);

fp = fopen(file, "r");
while ((c = getc(fp)) != EOF) putchar(c);
fclose(fp);

return 0;
}

I've left the testing of fopen(), etc. up to you.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '08 #5
On 13 Nov 2008 at 21:43, Jason wrote:
On 13 Nov, 21:39, Nate Eldredge <n...@vulcan.la nwrote:
>You want the "r+" mode.

Ah that simple, thanks very much works perfectly.
You may also want to do an fsync() (or fclose() of course) after writing
the data, to make sure it gets committed to disk.

Nov 13 '08 #6
Joe Wright <jo********@com cast.netwrites:
Jason wrote:
>I need to open an existing file, seek to a position at X number of
bytes, and write out Y number of bytes overwriting any existing bytes,
but no erasing any other data. Is this possible?
[...]
Try this..

#include <stdio.h>
#include <stdlib.h>

int main(void) {
FILE *fp;
char *file = "jc.txt";
int c;

fp = fopen(file, "w");
fprintf(fp, "My name is jason.\n");
fclose(fp);

fp = fopen(file, "r");
while ((c = getc(fp)) != EOF) putchar(c);
fclose(fp);

fp = fopen(file, "r+");
fseek(fp, 11, SEEK_SET);
fputc('J', fp);
fclose(fp);

fp = fopen(file, "r");
while ((c = getc(fp)) != EOF) putchar(c);
fclose(fp);

return 0;
}

I've left the testing of fopen(), etc. up to you.
You almost certainly want to do this in binary mode.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 13 '08 #7
On Thu, 13 Nov 2008 17:27:24 -0500,
Joe Wright <jo********@com cast.netwrote:
Jason wrote:
>Hi,

I need to open an existing file, seek to a position at X number of
bytes, and write out Y number of bytes overwriting any existing bytes,
but no erasing any other data. Is this possible?
Try this..
fp = fopen(file, "r+");
fseek(fp, 11, SEEK_SET);
fputc('J', fp);
fclose(fp);
To remain portable, the second argument of fseek() has to be 0 or a value
returned by a previous call to ftell(). You should probably open the
file in binary mode.

Martien
--
| Some people, when confronted with a problem,
Martien Verbruggen | think: "I know, I'll use regular
| expressions". Now they have two problems. --
| Jamie Zawinski
Nov 13 '08 #8
Keith Thompson wrote:
Joe Wright <jo********@com cast.netwrites:
>Jason wrote:
>>I need to open an existing file, seek to a position at X number of
bytes, and write out Y number of bytes overwriting any existing bytes,
but no erasing any other data. Is this possible?
[...]
>Try this..

#include <stdio.h>
#include <stdlib.h>

int main(void) {
FILE *fp;
char *file = "jc.txt";
int c;

fp = fopen(file, "w");
fprintf(fp, "My name is jason.\n");
fclose(fp);

fp = fopen(file, "r");
while ((c = getc(fp)) != EOF) putchar(c);
fclose(fp);

fp = fopen(file, "r+");
fseek(fp, 11, SEEK_SET);
fputc('J', fp);
fclose(fp);

fp = fopen(file, "r");
while ((c = getc(fp)) != EOF) putchar(c);
fclose(fp);

return 0;
}

I've left the testing of fopen(), etc. up to you.

You almost certainly want to do this in binary mode.
It never occurred to me. Why on earth use binary mode?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '08 #9
Joe Wright <jo********@com cast.netwrites:
Keith Thompson wrote:
>Joe Wright <jo********@com cast.netwrites:
>>Jason wrote:
I need to open an existing file, seek to a position at X number of
bytes, and write out Y number of bytes overwriting any existing bytes,
but no erasing any other data. Is this possible?
[...]
>>Try this..

#include <stdio.h>
#include <stdlib.h>

int main(void) {
FILE *fp;
char *file = "jc.txt";
int c;

fp = fopen(file, "w");
fprintf(fp, "My name is jason.\n");
fclose(fp);

fp = fopen(file, "r");
while ((c = getc(fp)) != EOF) putchar(c);
fclose(fp);

fp = fopen(file, "r+");
fseek(fp, 11, SEEK_SET);
fputc('J', fp);
fclose(fp);

fp = fopen(file, "r");
while ((c = getc(fp)) != EOF) putchar(c);
fclose(fp);

return 0;
}

I've left the testing of fopen(), etc. up to you.
You almost certainly want to do this in binary mode.
It never occurred to me. Why on earth use binary mode?
For one thing, your fseek() call isn't guaranteed to be meaningful for
a text file; the behavior is undefined unless the offset is either
zero or a value obtained from an earlier call to ftell().

You'll probably get away with it if you (a) use fseek and ftell
carefully, and (b) avoid either overwriting or creating any new-lines,
but there could still be some issues.

On Unix-like systems, it doesn't make any difference; on Windows, a
new-line is represented as a two-character sequence, and other systems
have even stranger ways of representing lines in text files.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 13 '08 #10

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

Similar topics

1
7388
by: Ellixis | last post by:
Hello, How can I use fwrite() and fseek() in order to write data in the middle (or anywhere else) of a file without overwriting existing data ? People told me that I should load the file into memory (a variable) and use concatenation. But, according to me, It isn't clean to load an entire file into
5
2719
by: John Bokma | last post by:
I want to use file_get_contents, which according to the documentation returns FALSE if it fails. However, it also fails if I read a file of 0 bytes in size. So I tried to use filesize to check first. Same problem, I seem not to be able to distinguish between a filesize of 0 bytes and FALSE. What am I doing wrong? --
12
3181
by: Yandos | last post by:
Hello all, why is the file 257 bytes long in windows xp, when it contains only 256 characters?: #include <stdio.h> int main(void) { int i; FILE *out; if ((out = fopen("256.tmp", "w")) == NULL) {
11
9884
by: Steven D'Aprano | last post by:
I want to rename a file, but only if the destination file name doesn't already exist. I can do this: if os.path.exists(dest): # skip file, raise an exception, make a backup... do_something_else() else: os.rename(src, dest)
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10163
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9037
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6779
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.