0

How can I truncate a file with the given size and if the filesize is smaller then the given size how can I enlarge the existing file size with 0 padding.

Thanks for the help.

user503403
  • 259
  • 1
  • 2
  • 14
  • Not a real dupes, as this question is about resizing a file in general, not only about shrinking. – alk Aug 21 '13 at 06:17

3 Answers3

1

To skrink use truncate() or ftruncate(), to enlarge use lseek()/fseek() followed by write()/fwrite()ing one byte.

alk
  • 69,737
  • 10
  • 105
  • 255
0

This may help you with truncation:

How to truncate a file in C?

And I do not think you can enlarge a file without some kind of padding (it depends on how you view "padding").

Community
  • 1
  • 1
Joohwan
  • 2,374
  • 1
  • 19
  • 30
0

It has been solved in this How to truncate a file in C?
Basically, you can use these two functions:

#include <unistd.h>
int ftruncate(int fildes, off_t length);
int truncate(const char *path, off_t length);
Community
  • 1
  • 1
Jin Chen
  • 632
  • 1
  • 4
  • 10