Author |
Message |
Pieter Dawso #1 / 17
|
 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 |
|
 |
Joerg Schoe #2 / 17
|
 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 |
|
 |
Dan P #3 / 17
|
 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 |
|
 |
revolution.. #4 / 17
|
 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 |
|
 |
Jens M Andrease #5 / 17
|
 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 |
|
 |
Gareth Hughe #6 / 17
|
 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 |
|
 |
Tim Hollebe #7 / 17
|
 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 |
|
 |
Dominic Feel #8 / 17
|
 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 |
|
 |
Coridon Hensh #9 / 17
|
 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 |
|
 |
Lawrence Kir #10 / 17
|
 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 |
|
 |
Lawrence Kir #11 / 17
|
 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 |
|
 |
NF Steve #12 / 17
|
 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 |
|
 |
Jens Schweikhard #13 / 17
|
 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 |
|
 |
Benz #14 / 17
|
 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 |
|
 |
Yngvar Folli #15 / 17
|
 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 |
|
|