How to crop a file to the desired size in Windows?


It is to crop, not to copy partially. Accordingly, Windows does not have rich tools for all occasions, such as in Fra:

truncate file.goy

We need such a thing for the test from under the MSBiuld script.


I don’t know for the script, but for C (MinGW gcc) there is a ftruncate () function that allows you to truncate the file.

The simplest program:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

main (int ac, char *av[])
  if (ac < 3) {
    fprintf (stderr,"Invalid usage\nUse: truncate SIZE FILE\n");
    exit (1);
  off_t size = atoi(av[1]);
  if (size < 0) // здесь можно что-то другое, например с конца файла
    size = 0;
  FILE *f = fopen(av[2],"r+");
  if (!f) {
    exit (-1);
  if (ftruncate(fileno(f), size)) {
    exit (-1);
  exit (0);
Scroll to Top