How to truncate a file in C?

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 – … Read more

Truncate a string to first n characters of a string and add three dots if any characters are removed

//The simple version for 10 Characters from the beginning of the string $string = substr($string,0,10).’…’; Update: Based on suggestion for checking length (and also ensuring similar lengths on trimmed and untrimmed strings): $string = (strlen($string) > 13) ? substr($string,0,10).’…’ : $string; So you will get a string of max 13 characters; either 13 (or less) … Read more