How to truncate a file from the end? (cross platform)

后端 未结 3 1528
别跟我提以往
别跟我提以往 2021-01-19 18:49

I am trying to find a cross-platform method to delete X bytes from the end of a file.

Currently I have found:

  • Platform specific solutio

相关标签:
3条回答
  • 2021-01-19 19:16

    The Boost.Filesystem library (version 1.44 and above) provides a resize_file function.

    0 讨论(0)
  • 2021-01-19 19:22

    How do you think cross platform functions work? Just make your own function like this:

    int truncate(int fd, long size)
    {
    #ifdef _WIN32 || _WIN64 
        return _chsize(fd, size);
    #else
      #ifdef POSIX
        return ftruncate(fd, size);
      #else
        // code for other OSes
      #endif
    #endif
    }
    
    0 讨论(0)
  • 2021-01-19 19:25

    There is no such thing in the Standard library. They support streams- but streams don't have ends, files happen to have ends- at the current time.

    All you can do is write a cross-platform wrapper on the function.

    0 讨论(0)
提交回复
热议问题