problem with fseek

Z

Zach

I am trying to use fseek to move file pointer position to the very
beginning of the file and write "<HTML>" and then move to the very end
and write "</HTML>" but instead it is writing the "<HTML>" at the end
of the first line of the file instead of at the beginning of the first
line. From everything I've read this *should* work. Hope someone can
tell me what's wrong.

first line of "test.log" is:
This is line 1 of test file.

So after I run the code it produces:
This is line 1 of test file.<HTML>
</HTML>

Code:

FILE *fp;

fp = fopen("test.log","a+");
fseek(fp, 0, SEEK_SET);
fprintf(fp,"<HTML>\n");
fseek (fp , 0 , SEEK_END);
fprintf(fp,"</HTML>\n");
fclose(fp);

Zach
 
Z

Zach

I am trying to use fseek to move file pointer position to the very
beginning of the file and write "<HTML>" and then move to the very end
and write "</HTML>" but instead it is writing the "<HTML>" at the end
of the first line of the file instead of at the beginning of the first
line. From everything I've read this *should* work. Hope someone can
tell me what's wrong.

first line of "test.log" is:
This is line 1 of test file.

So after I run the code it produces:
This is line 1 of test file.<HTML>
</HTML>

Code:

      FILE *fp;

      fp = fopen("test.log","a+");
      fseek(fp, 0, SEEK_SET);
      fprintf(fp,"<HTML>\n");
      fseek (fp , 0 , SEEK_END);
      fprintf(fp,"</HTML>\n");
      fclose(fp);

Zach

I changed the mode from "a+" to "w+" and now it writes the <HTML> to
the beginning of the file, but overwrites the existing data!

<HTML>\ns line one.
</HTML>

Zach
 
I

Ian Collins

I changed the mode from "a+" to "w+" and now it writes the<HTML> to
the beginning of the file, but overwrites the existing data!

<HTML>\ns line one.
</HTML>

That is exactly what you are telling it to do. There isn't an insert
mode for writing files, data is either appended (as in your first
example) or over written. If you wish to insert data, you have to read
the file, modify the data and then write it back.
 
Z

Zach

That is exactly what you are telling it to do.  There isn't an insert
mode for writing files, data is either appended (as in your first
example) or over written.  If you wish to insert data, you have to read
the file, modify the data and then write it back.

Ah ok. Too bad C's standard library doesn't have insert functionality.

Zach
 
K

Kenny McCormack

Ah ok. Too bad C's standard library doesn't have insert functionality.

Zach

Quite so. For that, the only OS/filesystem combination that I know of
is MTS (Michigan Terminal System).

--
Windows 95 n. (Win-doze): A 32 bit extension to a 16 bit user interface for
an 8 bit operating system based on a 4 bit architecture from a 2 bit company
that can't stand 1 bit of competition.

Modern day upgrade --> Windows XP Professional x64: Windows is now a 64 bit
tweak of a 32 bit extension to a 16 bit user interface for an 8 bit
operating system based on a 4 bit architecture from a 2 bit company that
can't stand 1 bit of competition.
 
E

Eric Sosman

Ah ok. Too bad C's standard library doesn't have insert functionality.

C's library lacks insertion capability because almost all file
systems do, too. In four-plus decades of writing programs, I have
encountered exactly one file system that could do insertions in
sequential files. Even then, the insertion couldn't be of arbitrary
size: It had to be so-and-so-many disk blocks.
 
S

Seebs

Ah ok. Too bad C's standard library doesn't have insert functionality.

It's not a question of the library, but of the filesystem.

Imagine that you have a five gigabyte file. You now want to insert
a character at the beginning. You get to rewrite all five gigabytes.
Now you want to insert another character...

-s
 
S

Seebs

I am trying to use fseek to move file pointer position to the very
beginning of the file and write "<HTML>" and then move to the very end
and write "</HTML>"

There is no "insert" mode in standard C (or in any filesystem I've
ever seen).

Meaning you cannot do what you probably *want* to do, which is insert
that *before* the current start of the file.

-s
 
D

Dann Corbit

Ah ok. Too bad C's standard library doesn't have insert functionality.

Store the log as a database file and then you can insert, update and
delete with impunity.

Some NOSQL database systems operate at near operating system file speed.
You might enjoy reading this:
http://fallabs.com/kyotocabinet/

A simpler solution might be to create a file that contains:
<HTML>
called start.tag and a second file that contains:
</HTML> called end.tag and then concatenate the three files to a target
file. Of course, this is slower and wastes space, but at least it is
simple.
 
K

Keith Thompson

Seebs said:
It's not a question of the library, but of the filesystem.

Imagine that you have a five gigabyte file. You now want to insert
a character at the beginning. You get to rewrite all five gigabytes.
Now you want to insert another character...

How difficult and/or this is depends on the underlying filesystem
implementation.

Some filesystems [1] make it difficult or impossible to do this
directly. Others [2] allow insertions and deletions at arbitrary
positions, implicitly updating the offsets of any following bytes
while leaving them in the same place on the physical disk.

[1] all of them [3]
[2] none of them [3]

[3] that I've heard of
 
R

robertwessel2

Store the log as a database file and then you can insert, update and
delete with impunity.


How would you key such a thing? Consider a table with two records, A
and B. Now you insert C and D between A and B. Then E and F between
C and D, and G and H between E and F... Each iteration of that
requires at least a couple of additional bits of key to record the
proper position. Perhaps if your database allowed an arbitrary length
ASN.1 style key (do any?).
 
K

Kenny McCormack

Keith Thompson said:
Some filesystems [1] make it difficult or impossible to do this
directly. Others [2] allow insertions and deletions at arbitrary
positions, implicitly updating the offsets of any following bytes
while leaving them in the same place on the physical disk.

[1] all of them [3]
[2] none of them [3]

You've obviously never heard of MTS...
 
T

Tom St Denis

Some filesystems [1] make it difficult or impossible to do this
directly.  Others [2] allow insertions and deletions at arbitrary
positions, implicitly updating the offsets of any following bytes
while leaving them in the same place on the physical disk.
[1] all of them [3]
[2] none of them [3]

You've obviously never heard of MTS...

An obscure OS used at a university in the 70s? Gosh, I wonder why
that wasn't covered in my comp.sci degree...

Tom
 
K

Kenny McCormack

Keith Thompson said:
Seebs said:
Ah ok. Too bad C's standard library doesn't have insert functionality.

It's not a question of the library, but of the filesystem.

Imagine that you have a five gigabyte file. You now want to insert
a character at the beginning. You get to rewrite all five gigabytes.
Now you want to insert another character...

How difficult and/or this is depends on the underlying filesystem
implementation.

Some filesystems [1] make it difficult or impossible to do this
directly. Others [2] allow insertions and deletions at arbitrary
positions, implicitly updating the offsets of any following bytes
while leaving them in the same place on the physical disk.

[1] all of them [3]
[2] none of them [3]

[3] that I've heard of

Off topic. Not in the C Standard. Cant talk about it here.

Leader keith can talk about anything he wants to.

It's in the rulebook; you can look it up.

--

Some of the more common characteristics of Asperger syndrome include:

* Inability to think in abstract ways (eg: puns, jokes, sarcasm, etc)
* Difficulties in empathising with others
* Problems with understanding another person's point of view
* Hampered conversational ability
* Problems with controlling feelings such as anger, depression
and anxiety
* Adherence to routines and schedules, and stress if expected routine
is disrupted
* Inability to manage appropriate social conduct
* Delayed understanding of sexual codes of conduct
* A narrow field of interests. For example a person with Asperger
syndrome may focus on learning all there is to know about
baseball statistics, politics or television shows.
* Anger and aggression when things do not happen as they want
* Sensitivity to criticism
* Eccentricity
* Behaviour varies from mildly unusual to quite aggressive
and difficult
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

fseek 17
Help with EXT3 Filesystem work 1
Question about change of "fp" in function "fseek"and "ftell" 3
fseek() 6
Determining EOF using fseek()? 6
fseek speed 16
URGENT 1
fseek and open_memstream 23

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top