473,804 Members | 3,396 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
27 7551
Ian Collins <ia******@hotma il.comwrites:
la************@ siemens.com wrote:
>In regards to C99: 7.19.9.2p4:

For a text stream, either offset shall be zero, or offset shall
be a value returned by an earlier successful call to the ftell
function on a stream associated with the same file and whence
shall be SEEK_SET.

Keith Thompson <ks***@mib.orgw rote:
>>I didn't find it ambiguous until you mentioned it. 8-)}

For the second interpretation, I think there would have to be a comma
after "the same file".

And no comma after "offset shall be zero". Because English doesn't have
grouping operators, the standard is very carefully punctuated and also
makes judicious use of words like "either" and "both" to indicate the
correct parsing.

In non-American English commas are seldom, if ever, placed before an and.
What about the Oxford comma? OUP and Fowler both recommend its use.
The Guardian's neutral, advising its use if it would aid understanding.
I'm a Brit, and I'm very pro the Oxford comma; this is surprising, as
I tend to disagree with Fowler practically every time he's used as
support for some contruct.

Phil
--
I tried the Vista speech recognition by running the tutorial. I was
amazed, it was awesome, recognised every word I said. Then I said the
wrong word ... and it typed the right one. It was actually just
detecting a sound and printing the expected word! -- pbhj on /.
Nov 14 '08 #21
la************@ siemens.com writes:
In regards to C99: 7.19.9.2p4:

For a text stream, either offset shall be zero, or offset shall
be a value returned by an earlier successful call to the ftell
function on a stream associated with the same file and whence
shall be SEEK_SET.

Keith Thompson <ks***@mib.orgw rote:
>>
I didn't find it ambiguous until you mentioned it. 8-)}

For the second interpretation, I think there would have to be a comma
after "the same file".

And no comma after "offset shall be zero". Because English doesn't have
grouping operators, the standard is very carefully punctuated and also
makes judicious use of words like "either" and "both" to indicate the
correct parsing.
To me, the most compelling argument is that if the other meaning were
intended the restriction on whence would surely come first: "For a
text stream, whence shall be SEEK_SET and either offset shall be zero
or offset shall be a value returned by a ...".

I think you can get rid of any trace of an alternate parse simply by
putting the whence restriction sooner:

For a text stream, either offset shall be zero, or whence shall be
SEEK_SET and offset shall be a value returned by an earlier
successful call to the ftell function on a stream associated with
the same file.

Now any attempt to make the "and offset shall be a value..." clause
apply to both parts of the preceding "or" results in a double
restriction on offset (it shall be zero and the result of ftell).
Readers naturally avoid such illogical parses.

I don't think the original is ambiguous, but it seems to have proved to
be so despite careful punctuation!

--
Ben.
Nov 14 '08 #22
Ben Bacarisse <be********@bsb .me.ukwrites:
[...]
To me, the most compelling argument is that if the other meaning were
intended the restriction on whence would surely come first: "For a
text stream, whence shall be SEEK_SET and either offset shall be zero
or offset shall be a value returned by a ...".
[...]

Better yet, "For a text stream, whence shall be SEEK_SET and offset
shall be either zero or a value returned by a ...".

(And yes, I'd probably put a comma after SEEK_SET.)

--
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 14 '08 #23
Ian Collins <ia******@hotma il.comwrote:
>
http://en.wikipedia.org/wiki/Serial_comma a fairly balanced account.
The standard does use serial commas, but the usage we're discussing
(mixed "and" and "or" clauses) isn't really serial.
--
Larry Jones

This sounds suspiciously like one of Dad's plots to build my character.
-- Calvin
Nov 15 '08 #24
Ben Bacarisse <be********@bsb .me.ukwrote:
>
I think you can get rid of any trace of an alternate parse simply by
putting the whence restriction sooner:

For a text stream, either offset shall be zero, or whence shall be
SEEK_SET and offset shall be a value returned by an earlier
successful call to the ftell function on a stream associated with
the same file.
That's better, but I think it would be better still to capture the "and"
between the "either" and the "or":

For a text stream, either whence shall be SEEK_SET and offset
shall be a value returned by an earlier successful call to the
ftell function on a stream associated with the same file, or
offset shall be zero.
--
Larry Jones

What a waste to be going to school on a morning like this. -- Calvin
Nov 15 '08 #25
la************@ siemens.com wrote:
Ian Collins <ia******@hotma il.comwrote:
>http://en.wikipedia.org/wiki/Serial_comma a fairly balanced account.

The standard does use serial commas, but the usage we're discussing
(mixed "and" and "or" clauses) isn't really serial.
Is there a standard or guidelines for the language in ISO standards?

--
Ian Collins
Nov 15 '08 #26
la************@ siemens.com writes:
Ben Bacarisse <be********@bsb .me.ukwrote:
>>
I think you can get rid of any trace of an alternate parse simply by
putting the whence restriction sooner:

For a text stream, either offset shall be zero, or whence shall be
SEEK_SET and offset shall be a value returned by an earlier
successful call to the ftell function on a stream associated with
the same file.

That's better, but I think it would be better still to capture the "and"
between the "either" and the "or":

For a text stream, either whence shall be SEEK_SET and offset
shall be a value returned by an earlier successful call to the
ftell function on a stream associated with the same file, or
offset shall be zero.
That does not works so well to my mind -- maybe simply because of the
of two short phases with such a long one in the middle. It seems
harder to hold them meaning until it is all resolved. However, I am
biased!

--
Ben.
Nov 16 '08 #27
Ian Collins <ia******@hotma il.comwrote:
>
Is there a standard or guidelines for the language in ISO standards?
Part 2 of the ISO/IEC Directives (Rules for the structure and drafting
of International Standard) provides some guidelines.
--
Larry Jones

I thought my life would seem more interesting with a musical
score and a laugh track. -- Calvin
Nov 17 '08 #28

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
2720
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
9893
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
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10332
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10320
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
10077
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
9150
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
7620
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
5521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4299
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
3
2991
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.