Truncating a file. 
Author Message
 Truncating a file.

I want to truncate X bytes off a file (starting from the beginning). Can
I do that without actually creating a new file and copying contents over
to it?
--



Sat, 13 Sep 2003 13:59:02 GMT  
 Truncating a file.

Quote:

> I want to truncate X bytes off a file (starting from the beginning). Can
> I do that without actually creating a new file and copying contents over
> to it?

You mean truncate from the _start_ of the file, leaving the tail intact?
No, you probably can't do that directly. Truncating from the end is
often possible, but system-dependent:
<http://www.eskimo.com/~scs/C-faq/q19.13.html>.

Richard
--



Mon, 15 Sep 2003 05:39:08 GMT  
 Truncating a file.

Quote:

> I want to truncate X bytes off a file (starting from the beginning). Can
> I do that without actually creating a new file and copying contents over
> to it?

In a portable and reliable manner? No. You'll definitely have to copy
all the contents around, since hardly any OS in existence allows you
to truncate the start of a file, natively.
--

Even if all the snow were burnt, ashes would remain.
--



Mon, 15 Sep 2003 05:51:30 GMT  
 Truncating a file.
Not without getting deep into the crufty and non-portable world of file
systems.

If you want to free the disk space, then you'll have to copy it somewhere
else. If you just want ti to appear to start later on, then write your code
to seek as soon as it opens the file.


Quote:
> I want to truncate X bytes off a file (starting from the beginning). Can
> I do that without actually creating a new file and copying contents over
> to it?
> --


--



Mon, 15 Sep 2003 05:51:49 GMT  
 Truncating a file.

Quote:

> I want to truncate X bytes off a file (starting from the beginning). Can
> I do that without actually creating a new file and copying contents over
> to it?

Nothing portable that I know of.

However, your system may happen to support it, and I have seen systems
that have a "chsize()" system call.

Check with your particular C compiler's documentation.

--

+---------+----------------------------------+-----------------------------+

|    J.   |                                  |  herein are not necessarily |
|  Brody  | http://www.bestweb.net/~kenbrody |  those of fP Technologies." |
+---------+----------------------------------+-----------------------------+
GCS (ver 3.12) d- s+++: a C++$(+++) ULAVHSC^++++$ P+>+++ L+(++) E-(---)

    DI+(++++) D---() G e* h---- r+++ y?
--



Mon, 15 Sep 2003 09:52:37 GMT  
 Truncating a file.

Quote:

> I want to truncate X bytes off a file (starting from the beginning).
> Can I do that without actually creating a new file and copying
> contents over to it?

That depends on the operating system; there is no standard way to
do that, and some systems don't support it.
--



Mon, 15 Sep 2003 10:16:20 GMT  
 Truncating a file.

Quote:

> I want to truncate X bytes off a file (starting from the beginning). Can
> I do that without actually creating a new file and copying contents over
> to it?

Definitely maybe.  Depends on what address boundaries are acceptable for
files to start at.  You could simply write something like:

    FILE * myfile;
    myfile = fopen("a_file", rb+);
    myfile += FILEOFFSET;

or a more general way which would handle any value of X would be:

    int filesize, byte_counter;
    FILE * myfile;
    char * from_char, * to_char;
    myfile = fopen("a_file", rb+);
    filesize = sizeof myfile;
    to_char = (char *)myfile;
    from_char = (char *)(myfile + X);

    for (byte_counter = 0; X + byte_counter < filesize; byte_counter++){
        *to_char++ = *from_char++;
    }

Then you'll have copied the file named a_file in place, and shortened it.

--Jeff Turner
--



Mon, 15 Sep 2003 10:19:51 GMT  
 Truncating a file.

Quote:

> I want to truncate X bytes off a file (starting from the beginning). Can
> I do that without actually creating a new file and copying contents over
> to it?

It depends on your operating system -- check ftruncate() and truncate(),
and beware 64-bit file systems.  Note that you'd also have to establish
the current size of the file and compute the new size; these functions
do not chop X bytes off the end but set the total size of the file to N
bytes (from the beginning).

--
Yours,

Guardian of DBD::Informix v1.00.PC1 -- http://www.perl.com/CPAN
     "I don't suffer from insanity; I enjoy every minute of it!"
--



Mon, 15 Sep 2003 10:21:48 GMT  
 Truncating a file.

Yes I ended up doing something similar. One additional thing required is
ftruncate to actually get rid of the
superfluous end bit which we have allready copied. I couldn't work out
any other way to put an eof  where i want to.

Anurag

Quote:


>> I want to truncate X bytes off a file (starting from the beginning). Can
>> I do that without actually creating a new file and copying contents over
>> to it?

> Definitely maybe.  Depends on what address boundaries are acceptable for
> files to start at.  You could simply write something like:

>     FILE * myfile;
>     myfile = fopen("a_file", rb+);
>     myfile += FILEOFFSET;

> or a more general way which would handle any value of X would be:

>     int filesize, byte_counter;
>     FILE * myfile;
>     char * from_char, * to_char;
>     myfile = fopen("a_file", rb+);
>     filesize = sizeof myfile;
>     to_char = (char *)myfile;
>     from_char = (char *)(myfile + X);

>     for (byte_counter = 0; X + byte_counter < filesize; byte_counter++){
>         *to_char++ = *from_char++;
>     }

> Then you'll have copied the file named a_file in place, and shortened it.

> --Jeff Turner



Wed, 17 Sep 2003 07:56:14 GMT  
 Truncating a file.
I hope this one was a joke...

Quote:


> > I want to truncate X bytes off a file (starting from the beginning). Can
> > I do that without actually creating a new file and copying contents over
> > to it?

> Definitely maybe.  Depends on what address boundaries are acceptable for
> files to start at.  You could simply write something like:

>     FILE * myfile;
>     myfile = fopen("a_file", rb+);
>     myfile += FILEOFFSET;

> or a more general way which would handle any value of X would be:

>     int filesize, byte_counter;
>     FILE * myfile;
>     char * from_char, * to_char;
>     myfile = fopen("a_file", rb+);
>     filesize = sizeof myfile;
>     to_char = (char *)myfile;
>     from_char = (char *)(myfile + X);

>     for (byte_counter = 0; X + byte_counter < filesize; byte_counter++){
>         *to_char++ = *from_char++;
>     }

> Then you'll have copied the file named a_file in place, and shortened it.

> --Jeff Turner
> --


--



Wed, 17 Sep 2003 07:57:27 GMT  
 Truncating a file.

Quote:

> Yes I ended up doing something similar.

Warning.  I hope you didn't do anything like what Jeffrey Turner wrote,
as it had so many mistakes that I seriously wonder if he was just
pulling your leg.  You can *not* get the file size with the "sizeof"
operator.  And you can *not* just cast a file pointer to char *, and
expect to read or write to the file just by dereferencing the pointer.

If you want to move the contents of the file, you have to do it with the
normal library functions, such as getc/putc or fread/fwrite.  Also, if
you want to read from one place of the file and write to another, you
have to repeatedly save the current position with the fgetpos function,
and set it with fsetpos.  This can drastically slow down the program
unless you move large chunks of data at the time.

Quote:
>  One additional thing required is
> ftruncate to actually get rid of the
> superfluous end bit which we have allready copied. I couldn't work out
> any other way to put an eof  where i want to.

But ftruncate is not standard.  And because of the necessary saving and
setting of file positions, it may not be worth it anyway.

I think that all in all, the best way to do what you want is the obvious
one.  Create a new file with a temporary file name.  Start reading the
original file from the position you want, and write it to the new file.
Close both files when finished, delete the original file, and rename the
new file to the original name.  But before you delete the original file,
make sure that none of the operations you've done, including the close,
have resulted in an error.

This can be done using standard functions, and I don't think it would be
much slower than any of the other solutions suggested here since, as has
been said, I don't know *any* implementations that allow truncating away
the *beginning* of a file.

Yngvar
--



Tue, 23 Sep 2003 06:22:43 GMT  
 Truncating a file.
It didn't take me long to realise what Jeffrey was suggesting was way
off the mark :-)
May be he was trying an April fools{*filter*} on me :P.

I did end up copying large chunks and then truncating the file where
required. I agree ftruncate
is not portable, but thats not a big issue for the software I am developing.

Thanks kindly to everyone for for there responses.  Hope I can be of
some help to you guys as well.

Kind regards,

Anurag

Quote:



>> Yes I ended up doing something similar.

> Warning.  I hope you didn't do anything like what Jeffrey Turner wrote,
> as it had so many mistakes that I seriously wonder if he was just
> pulling your leg.  You can *not* get the file size with the "sizeof"
> operator.  And you can *not* just cast a file pointer to char *, and
> expect to read or write to the file just by dereferencing the pointer.

> If you want to move the contents of the file, you have to do it with the
> normal library functions, such as getc/putc or fread/fwrite.  Also, if
> you want to read from one place of the file and write to another, you
> have to repeatedly save the current position with the fgetpos function,
> and set it with fsetpos.  This can drastically slow down the program
> unless you move large chunks of data at the time.

>>  One additional thing required is
>> ftruncate to actually get rid of the
>> superfluous end bit which we have allready copied. I couldn't work out
>> any other way to put an eof  where i want to.

> But ftruncate is not standard.  And because of the necessary saving and
> setting of file positions, it may not be worth it anyway.

> I think that all in all, the best way to do what you want is the obvious
> one.  Create a new file with a temporary file name.  Start reading the
> original file from the position you want, and write it to the new file.
> Close both files when finished, delete the original file, and rename the
> new file to the original name.  But before you delete the original file,
> make sure that none of the operations you've done, including the close,
> have resulted in an error.

> This can be done using standard functions, and I don't think it would be
> much slower than any of the other solutions suggested here since, as has
> been said, I don't know *any* implementations that allow truncating away
> the *beginning* of a file.

> Yngvar

--



Tue, 23 Sep 2003 20:39:13 GMT  
 Truncating a file.


Quote:

>> I want to truncate X bytes off a file (starting from the beginning). Can
>> I do that without actually creating a new file and copying contents over
>> to it?

>Definitely maybe.  Depends on what address boundaries are acceptable for
>files to start at.  You could simply write something like:

>    FILE * myfile;
>    myfile = fopen("a_file", rb+);

I think you mean "rb+", but that seems a rather minor thing to point out.
:-)

Quote:
>    myfile += FILEOFFSET;

>or a more general way which would handle any value of X would be:

>    int filesize, byte_counter;
>    FILE * myfile;
>    char * from_char, * to_char;
>    myfile = fopen("a_file", rb+);
>    filesize = sizeof myfile;
>    to_char = (char *)myfile;
>    from_char = (char *)(myfile + X);

Interesting technique. :-) Although if somebody is going to use ftruncate()
or similar they might also consider using mmap() or similar.

Quote:
>    for (byte_counter = 0; X + byte_counter < filesize; byte_counter++){
>        *to_char++ = *from_char++;
>    }

And there is always memmove().

Quote:
>Then you'll have copied the file named a_file in place, and shortened it.

With a bit of imagination. :-)

--
-----------------------------------------


-----------------------------------------
--



Mon, 29 Sep 2003 00:50:33 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. How can I truncate the file ?

2. Truncating a file

3. truncating a file

4. how to truncate a file?

5. Truncate a file ?

6. truncate a file (again!)

7. How to truncate a file?

8. Truncating a file ??

9. How do you truncate a file?

10. Truncate a File?

11. Truncate a file?

12. how to truncate a file opened in text mode

 

 
Powered by phpBB® Forum Software