Truncate a file? 
Author Message
 Truncate a file?

Is there a library function that truncates a file? say, through away the
last x bytes of the file. Can't seem to find such function in my c-book.

I am using Turbo C++ 3.0

Thanks a lot.

Raymond



Sun, 15 Jun 1997 15:01:25 GMT  
 Truncate a file?

Quote:
> Is there a library function that truncates a file? say, through away the
> last x bytes of the file. Can't seem to find such function in my c-book.

    There isn't a standard library function to do this. Some operating
    systems have lower level functions which can be used to truncate
    files, but generally you have to overwrite the file you want
    truncated.

    If the file is specific to your application, you could probably
    ignore the truncation problem by keeping a seperate size value
    somewhere, and then using fseek() to move to where the logical end
    of the file is supposed to be before writing to it.

Quote:
> Raymond

--

***    "Radioactivity - It's in the air, for you and me" - Kraftwerk    ***


Sun, 15 Jun 1997 19:57:02 GMT  
 Truncate a file?

Quote:
>Is there a library function that truncates a file?

If by truncate you mean replace by a zero length file of the same name,
you can use:

        fopen(filename, "w");

from the standard library.

Quote:
>say, through away the
>last x bytes of the file.

If x < filesize, there's no portable way to do it.
--
Wayne Berke



Mon, 16 Jun 1997 05:28:43 GMT  
 Truncate a file?

: Is there a library function that truncates a file? say, through away the
: last x bytes of the file. Can't seem to find such function in my c-book.

: I am using Turbo C++ 3.0

Yes, the other guys just said it: there's no portable way. But if you
don't care about portability you may use Borland's `chsize' function
defined in <io.h> which does exactly what you want.

pm



Wed, 18 Jun 1997 20:32:18 GMT  
 Truncate a file?

Quote:


>: Is there a library function that truncates a file? say, through away the
>: last x bytes of the file. Can't seem to find such function in my c-book.

>: I am using Turbo C++ 3.0

>Yes, the other guys just said it: there's no portable way. But if you
>don't care about portability you may use Borland's `chsize' function
>defined in <io.h> which does exactly what you want.

>pm

The following is also an option, and as far as I know is portable. The
example assumes there's a file on disk named 'xxx'. Of course, you'd want to
put some error checking into a real version.

#include <stdio.h>

#define truncateFile(x) {FILE *fp; fp=fopen(x,"w"); fclose(fp);}

int main()
{
  truncateFile("xxx");
  return 0;

Quote:
}

--
Joe Halpin

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


Wed, 18 Jun 1997 20:52:47 GMT  
 Truncate a file?


: >
: >: Is there a library function that truncates a file? say, through away the
                                                             ^^^^^^^^^^^^^^^^
: >: last x bytes of the file. Can't seem to find such function in my c-book.
     ^^^^^^^^^^^^^

: #include <stdio.h>

: #define truncateFile(x) {FILE *fp; fp=fopen(x,"w"); fclose(fp);}

: int main()
: {
:   truncateFile("xxx");
:   return 0;
: }

  Not quite the same thing as throwing away the last so many bytes of a file
  though is it ?  That is unless the length of the file is less than x bytes!
--
 Dave Halverson, Wanganui N.Z.



Thu, 19 Jun 1997 21:17:06 GMT  
 Truncate a file?

Quote:
>: >: Is there a library function that truncates a file? say, through away the
>: >: last x bytes of the file. Can't seem to find such function in my c-book.
>: #include <stdio.h>
>: #define truncateFile(x) {FILE *fp; fp=fopen(x,"w"); fclose(fp);}
>: int main()
>: {
>:   truncateFile("xxx");
>:   return 0;
>: }

Isn't it just going to make the length of the file 0???


Fri, 20 Jun 1997 02:39:13 GMT  
 Truncate a file?

Quote:

>Is there a library function that truncates a file? say, through away the
>last x bytes of the file. Can't seem to find such function in my c-book.

This is covered in the FAQ.  Write to me if you can't find it.

--



Sat, 21 Jun 1997 04:58:41 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Truncating a file.

2. How can I truncate the file ?

3. Truncating a file

4. truncating a file

5. how to truncate a file?

6. Truncate a file ?

7. truncate a file (again!)

8. How to truncate a file?

9. Truncating a file ??

10. How do you truncate a file?

11. Truncate a File?

12. how to truncate a file opened in text mode

 

 
Powered by phpBB® Forum Software