If you want to preserve the previous contents of the file up to some length (a length bigger than zero, which other answers provide), then POSIX provides the truncate() and ftruncate() functions for the job.

#include <unistd.h>
int ftruncate(int fildes, off_t length);
int truncate(const char *path, off_t length);

The name indicates the primary purpose - shortening a file. But if the specified length is longer than the previous length, the file grows (zero padding) to the new size. Note that ftruncate() works on a file descriptor, not a FILE *; you could use:

if (ftruncate(fileno(fp), new_length) != 0) ...error handling...

However, you should be aware that mixing file stream (FILE *) and file descriptor (int) access to a single file is apt to lead to confusion — see the comments for some of the issues. This should be a last resort.

It is likely, though, that for your purposes, truncate on open is all you need, and for that, the options given by others will be sufficient.


For Windows, there is a function SetEndOfFile() and a related function SetFileValidData() function that can do a similar job, but using a different interface. Basically, you seek to where you want to set the end of file and then call the function.

There's also a function _chsize() as documented in the answer by sofr.

Answer from Jonathan Leffler on Stack Overflow
🌐
W3Schools
w3schools.com › c › ref_math_trunc.php
C Math trunc() Function
C Examples C Real-Life Examples ... trunc(-5.9)); Try it Yourself » · The trunc() function truncates a number, which means returning only the integer part of the number....
🌐
GeeksforGeeks
geeksforgeeks.org › c language › trunc-truncf-truncl-c-language
trunc() , truncf() , truncl() in C language - GeeksforGeeks
February 6, 2023 - Syntax : long double truncl (long double x); Parameters: x :It takes a long double value as an input and then truncates the values after the decimal point. Return Value : It returns a decimal value whose values after the decimal point is 0 only.
🌐
Linux Man Pages
man7.org › linux › man-pages › man2 › truncate.2.html
truncate(2) - Linux manual page
The truncate() and ftruncate() ... fd to be truncated to a size of precisely length bytes. If the file previously was larger than this size, the extra data is lost. If the file previously was shorter, it is extended, and the extended part reads as null bytes ('\0'). The file offset is not changed. If the size changed, then the st_ctime and st_mtime fields (respectively, time of last status change and time of last modification; see inode(7)) for the ...
🌐
TutorialsPoint
tutorialspoint.com › trunc-truncf-truncl-in-c-language
trunc() , truncf() , truncl() in C language
Here we will see three functions. These functions are trunc(), truncf() and the truncl(). These functions are used to convert floating point values into truncated form. The trunc() Function This function is used to truncate double ty
🌐
Fresh2Refresh
fresh2refresh.com › home › c programming tutorial › c – arithmetic functions › c – trunc() function
C trunc() function | C Arithmetic functions | Fresh2Refresh
September 23, 2020 - C trunc() function:trunc( ) function in C truncates the decimal value from floating point value and returns integer value.”math.h” header
Find elsewhere
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › c/c++
Truncate a string in C - Raspberry Pi Forums
I need to change the .bin extension on the input file name to .loc. Scratching around in my memory for what I learned about C in the 1980's, it seemed the simplest way would be to find the last "." in the input filename string, truncate it at the next character by writing '\0' to it, then concatenating it with "loc" thus:
🌐
YouTube
youtube.com › watch
truncate() Function To Truncate A File (POSIX Library) | C Programming Tutorial - YouTube
How to use the truncate() function in the POSIX library to truncate a file in C. Source code: https://github.com/portfoliocourses/c-example-code/blob/main/tr...
Published   April 7, 2022
🌐
Reddit
reddit.com › r/c_programming › how can i safely truncate a signed integer?
r/C_Programming on Reddit: How can I safely truncate a signed integer?
February 27, 2022 -

How can I safely convert a long to int by discarding all bits beyond 33? If I simply cast, if the value in the long cannot be represented in int (i.e. overflows), the behavior is undefined. How can I get around this? I could cast them to unsigned prior to truncation, but then how can I safely convert unsigned int to int without invoking undefined behavior if the MSB is set? Is the following undefined behavior:

(int)(unsigned int)(int){-1};
🌐
YouTube
youtube.com › watch
Truncate A String | C Programming Example - YouTube
How to truncate a string using C. Source code: https://github.com/portfoliocourses/c-example-code/blob/main/string_truncate.c. Check out https://www.portfo...
Published   September 21, 2022
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › trunc-truncf-truncl
trunc, truncf, truncl | Microsoft Learn
July 9, 2025 - Determines the nearest integer that is less than or equal to the specified floating-point value. double trunc( double x ); long double truncl( long double x ); #define trunc(X) // Requires C11 or later long double trunc( long double x ); //C++ ...
🌐
Delft Stack
delftstack.com › home › howto › truncate string in c
How to Truncate String in C | Delft Stack
March 12, 2025 - One of the simplest methods to truncate a string in C is by using the strlen() function to determine the length of the string, followed by manual assignment to limit the string to a desired length.
🌐
The Open Group
pubs.opengroup.org › onlinepubs › 9699919799 › functions › truncate.html
truncate
The truncate() function shall cause the regular file named by path to have a size which shall be equal to length bytes. If the file previously was larger than length, the extra data is discarded. If the file was previously shorter than length, its size is increased, and the extended area appears ...
🌐
Medium
medium.com › @mayintuji › truncate-numbers-in-c-6c0d6cf4eff6
Truncate Numbers in C | by Mayin Dev | Medium
April 11, 2025 - This involves removing the fractional part of a floating-point number, leaving only the integer part. A simple way to achieve this in C is using type casting. For instance, casting a double to an int will truncate the fractional part.
🌐
Cppreference
en.cppreference.com › w › c › numeric › math › trunc
trunc, truncf, truncl - cppreference.com
May 23, 2024 - #include <math.h> #include <stdio.h> int main(void) { printf("trunc(+2.7) = %+.1f\n", trunc(+2.7)); printf("trunc(-2.7) = %+.1f\n", trunc(-2.7)); printf("trunc(-0.0) = %+.1f\n", trunc(-0.0)); printf("trunc(-Inf) = %+f\n", trunc(-INFINITY)); } ... Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/numeric/math/trunc&oldid=172067"
🌐
Cplusplus
cplusplus.com › reference › cmath › trunc
Cplusplus
double trunc ( double x); float trunc ( float x);long double trunc (long double x); double trunc (T x); // additional overloads for integral types
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 178407-unsigned-overflow-underflow-truncation-c.html
Unsigned Overflow/underflow/truncation - C
The value of c is the same because truncation removes the effect of overflow or underflow. The answer is the second one - I would like to understand why is it. At the first main - we have a variable of type int so when we divide it by 2 - and we get minus 0.5 - truncation happens - which leaves ...