TRUNCATE FILE: EOF? 
Author Message
 TRUNCATE FILE: EOF?

I am using G++ to try to truncate a file by writing an EOF (-1) to the
file at a given file position.  The program uses a homemade AFile class
to seek to the beginning of an open file; it then writes a -1 signed
char to the file in an attempt to truncate.  IT DOES NOT WORK!  Is there
a C/C++ function/library I can use to solve the problem?

Following is the source:

   // Go to beginning of fFile.
   fFile -> Seek(0);

   // used to write EOF (-1) to fFile.
   // required to make an array to use
   // fFile -> Write( int numbytes, void *data );
   signed char endOfFile[] = { -1 };

   // Write an end-of-file char to fFile.
   Boolean writeSuccess = fFile -> Write(1, endOfFile);

If anyone out there can provide some useful solutions, I would be
forever grateful!

Best Regards,

Troy Zirk

--



Sun, 05 Nov 2000 03:00:00 GMT  
 TRUNCATE FILE: EOF?

: I am using G++ to try to truncate a file by writing an EOF (-1) to the
: file at a given file position.  The program uses a homemade AFile class
: to seek to the beginning of an open file; it then writes a -1 signed
: char to the file in an attempt to truncate.  IT DOES NOT WORK!  Is there
: a C/C++ function/library I can use to solve the problem?

: Following is the source:
:  
:    // Go to beginning of fFile.
:    fFile -> Seek(0);

:    // used to write EOF (-1) to fFile.
:    // required to make an array to use
:    // fFile -> Write( int numbytes, void *data );
:    signed char endOfFile[] = { -1 };

:    // Write an end-of-file char to fFile.
:    Boolean writeSuccess = fFile -> Write(1, endOfFile);

: If anyone out there can provide some useful solutions, I would be

Truncating a file is OS-dependent, what you do is simply writing the
character -1 to the file, which results in the byte 255. If you want to
truncate the file to zero length, open it via (I only know ANSI-C, but
we are here in a newsgroup for that)

fopen(name,"w")

which implicitly truncates the file to zero size. Truncation to size!=0
must be done with functions like "truncate" in UNIX for example

: forever grateful!
  ^^^^^^^^^^^^^^^^^ I'll come back to that :)

Hope that helps, Joerg

--
                                                    \|/

------------------------------------------------oOO-(_)-OOo---------
        Joerg Schoen

Web-Page: http://www.pci.uni-heidelberg.de/tc/usr/joerg
--------------------------------------------------ooO-Ooo-----------
--



Sat, 11 Nov 2000 03:00:00 GMT  
 TRUNCATE FILE: EOF?


Quote:
>I am using G++ to try to truncate a file by writing an EOF (-1) to the
>file at a given file position.  The program uses a homemade AFile class
>to seek to the beginning of an open file; it then writes a -1 signed
>char to the file in an attempt to truncate.  IT DOES NOT WORK!

Why would you expect it to work?  Is it documented anywhere?

Quote:
>Is there a C/C++ function/library I can use to solve the problem?

Not in the standard C library.  The FAQ has some platform specific
suggestions.  For those who read it before posting :-)

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - PPE, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland
--



Sat, 11 Nov 2000 03:00:00 GMT  
 TRUNCATE FILE: EOF?

Quote:

> I am using G++ to try to truncate a file by writing an EOF (-1) to the
> file at a given file position.  The program uses a homemade AFile class

> Following is the source:

>    // Go to beginning of fFile.
>    fFile -> Seek(0);

I'm not familiar with this fFile class.  It's C++, not C, for one.  However,
does this fFile class have a data member that is the handle of the file?  If
so, you might be able to use the chsize() function, if your compiler
supports it.

--



Mon, 13 Nov 2000 03:00:00 GMT  
 TRUNCATE FILE: EOF?

:   signed char endOfFile[] = { -1 };

Uhm. For this to resemble what you probably had in mind try:

  int endOfFile[] = { -1 };

or perhaps:

  int endOfFile[] = { EOF };

All functions that handles single chars takes int as argument to cope with
special cases like EOF

 (
  )
c[]  // Jens M Andreasen

--



Mon, 13 Nov 2000 03:00:00 GMT  
 TRUNCATE FILE: EOF?

Quote:

> > I am using G++ to try to truncate a file by writing an EOF (-1) to the
> > file at a given file position.  The program uses a homemade AFile class

Have I understood you correctly - you are trying to write a -1 into the file to
truncate it at that point? I am not aware of an operating system that would
allow this to work. As I understand it most OS's do not store an end-of-file
marker in the file but rely on the FAT to determine when the end of the file has
been reached and then return EOF(-1) to indicate this. Even if you could write
-1 to the file this would not update the FAT with the new file size.

To truncate a file your best bet would be to read in to memory the data you want
to retain, delete the file and then write back the retained data to the
recreated file.

Gareth

--



Sat, 18 Nov 2000 03:00:00 GMT  
 TRUNCATE FILE: EOF?

Quote:


> > > I am using G++ to try to truncate a file by writing an EOF (-1) to the
> > > file at a given file position.  The program uses a homemade AFile class

> Have I understood you correctly - you are trying to write a -1 into the file to
> truncate it at that point? [...]

> To truncate a file your best bet would be to read in to memory the data you want
> to retain, delete the file and then write back the retained data to the
> recreated file.

man ftruncate
--



Mon, 20 Nov 2000 03:00:00 GMT  
 TRUNCATE FILE: EOF?



Quote:
> Subject: Re: TRUNCATE FILE:  EOF?

> To truncate a file your best bet would be to read in to memory the
> data you want to retain, delete the file and then write back the
> retained data to the recreated file.

This is a dangerous suggestion, taken on its own.
Who knows what might happen between you deleting the
file and you finally writing the last byte out from
memory to the new file.

However, the rename() function allows you a quick way to
get a bit more security.

1: Read the file into memory, or into a temporary file.
2: rename the original file.
3: Write out a "we've moved the original" marker, so you
   know the system is now inconsistent.
4: Write out the new version of the original file.
5: Clear the temporary file.
6: If possible, try to commit the changes to disk.
7: Clear the marker.

The making of a completely bomb-proof system is a bit more work.

--
Dominic Feeley
--



Mon, 20 Nov 2000 03:00:00 GMT  
 TRUNCATE FILE: EOF?


 > As I understand it most OS's do not store an end-of-file marker in the file
 > but rely on the FAT to determine when the end of the file has been reached
 > and then return EOF(-1) to indicate this.

'Most OSes' don't use FAT, but your point is valid.

 > To truncate a file your best bet would be to read in to memory the data you
 > want to retain, delete the file and then write back the retained data to
 > the recreated file.

There's two large problems with that method.  The first is that it is limited
to files small enough to fit into availible memory.  The second is that it is
highly unsafe if there is a risk of the system losing power (or otherwise
being interrupted) while the transfer is in progress.

It's far safer to copy the file's contents into a temporary file, then move
the temporary file over the original.

----------------------------------------------------------------------------
| This message does not reflect the views of Green Circle Communciations.  |
| OS/2 Warp - SCSI - free speech.  Do I pick losing causes or what?        |
| CGI, C/C++ and HTML spoken here.  http://www.myna.com/~gcircle/csbh.html |
--



Wed, 22 Nov 2000 03:00:00 GMT  
 TRUNCATE FILE: EOF?



Quote:




>> > > I am using G++ to try to truncate a file by writing an EOF (-1) to the
>> > > file at a given file position.  The program uses a homemade AFile class

EOF is a failure code generated by some I/O functions. Writing a failure code
to a file as a character makes very little sense.

Quote:
>> Have I understood you correctly - you are trying to write a -1 into the file
> to
>> truncate it at that point? [...]

>> To truncate a file your best bet would be to read in to memory the data you
> want
>> to retain, delete the file and then write back the retained data to the
>> recreated file.

As others have noted that would probably work (assuming that you have enough
memory) but it is dangerous if for any reason the operation faied before the
write could be successfully completed.

Quote:
>man ftruncate

ftruncate() is not a standard C library function. I don't see anything in
the question that asks for a Unix-specific solution (or even that a Unix
platform is being used, G++ isn't limited to Unix).

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


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



Wed, 22 Nov 2000 03:00:00 GMT  
 TRUNCATE FILE: EOF?



Quote:
>Have I understood you correctly - you are trying to write a -1 into the file to
>truncate it at that point? I am not aware of an operating system that would
>allow this to work.

The C language itself requires for example putchar(-1) to act the same
as putchar(UCHAR_MAX).

Quote:
>As I understand it most OS's do not store an end-of-file
>marker in the file but rely on the FAT to determine when the end of the file has>been reached and then return EOF(-1) to indicate this. Even if you could write
>-1 to the file this would not update the FAT with the new file size.

FAT is a DOS related monstrosity, most OSs use much better file block indexing
methods. You are correct, however that many systems maintain a separate file
size rather than put markers in the file data. In DOS I believe this size is
held in the file's directory entry, in Unix it is held in the file's i-node.

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


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



Wed, 22 Nov 2000 03:00:00 GMT  
 TRUNCATE FILE: EOF?

Quote:



>> Subject: Re: TRUNCATE FILE:  EOF?

>> To truncate a file your best bet would be to read in to memory the
>> data you want to retain, delete the file and then write back the
>> retained data to the recreated file.

>This is a dangerous suggestion, taken on its own.
>Who knows what might happen between you deleting the
>file and you finally writing the last byte out from
>memory to the new file.

>However, the rename() function allows you a quick way to
>get a bit more security.

>1: Read the file into memory, or into a temporary file.
>2: rename the original file.
>3: Write out a "we've moved the original" marker, so you
>   know the system is now inconsistent.
>4: Write out the new version of the original file.
>5: Clear the temporary file.
>6: If possible, try to commit the changes to disk.
>7: Clear the marker.

>The making of a completely bomb-proof system is a bit more work.

Completely bomb-proof is not possible portably but I would
suggest

1) Read file into memory and out to temporary file.
2) rename original
3) rename temporary to original name.

Norman
--
Any sufficiently stupid Usenet post is indistinguishable from a troll.
(with apologies to A Clarke)
--



Wed, 22 Nov 2000 03:00:00 GMT  
 TRUNCATE FILE: EOF?



#> >
#> > > I am using G++ to try to truncate a file by writing an EOF (-1) to the
#> > > file at a given file position.  The program uses a homemade AFile class
#> Have I understood you correctly - you are trying to write a -1 into the file to
#> truncate it at that point? [...]
#>
#> To truncate a file your best bet would be to read in to memory the data you want
#> to retain, delete the file and then write back the retained data to the
#> recreated file.

# man ftruncate

NAME
     truncate, ftruncate - set a file to a specified length

SYNOPSIS
     #include <unistd.h>

     int truncate(const char *path, off_t length);
     int ftruncate(int fildes, off_t length);
[...]

Oops. A Unix function. Are the moderators awake? :-)

[well, not entirely.  I'm still wrestling with how much of an off-topic
 *answer* is appropriate, etc etc. -mod]

Regards,

        Jens
--
Jens Schweikhardt  http://www.shuttle.de/schweikh
SIGSIG -- signature too long (core dumped)
--



Thu, 23 Nov 2000 03:00:00 GMT  
 TRUNCATE FILE: EOF?



Quote:




> > > > I am using G++ to try to truncate a file by writing an EOF (-1) to
the
> > > > file at a given file position.  The program uses a homemade AFile
class

> man ftruncate
> --

Doesn't that only work for real men... errr, Unix programmers?  I doubt
this will be much help on DOS...

--



Mon, 01 Jan 2001 03:00:00 GMT  
 TRUNCATE FILE: EOF?

Quote:



> > man ftruncate
> Doesn't that only work for real men... errr, Unix programmers?  I doubt
> this will be much help on DOS...

Borland C/C++ has a function called "chsize."  It will truncate if the
size is set smaller than the current size of the file.  Of course, it
isn't standard.

Yngvar
--



Sat, 06 Jan 2001 03:00:00 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 

1. EOF file checking for Txt file

2. When Is EOF not EOF?

3. Find eof without using EOF

4. EOF or not EOF

5. Truncating a file.

6. How can I truncate the file ?

7. Truncating large text files in C

8. Truncating an open file on UNIX

9. Truncating a file

10. truncating a file

11. how to truncate a file?

12. Truncate a file ?

 

 
Powered by phpBB® Forum Software