Trunc function in C under Linux

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

int main(int argc, const char* argv[])
{
    
    
    if(argc < 3)
    {
    
    
        fprintf(stderr, "%s <filename> <size>\n", argv[0]);
        exit(1);
    }

    long int len = strtol(argv[2], NULL, 10); 
    int  new_len = truncate(argv[1], len);

    if(new_len == -1)
    {
    
    
        perror("truncate");
        exit(1);
    }
    
    return 0;
}

Test Results

Insert picture description here

Guess you like

Origin blog.csdn.net/zxy131072/article/details/108534234