How to set the file length in C ++ Programming

Source: Internet
Author: User
Bool setfilelength (const char * filepath, off_t length) {# ifdef win32system: IO: filestream * file = system: IO: file: open (length, file :: openmode, file: allaccess); If (! File) {return false ;}/// <summary> // expression call file-> setlength () method // The underlying implementation of this method is to call ntsetinformationfile // and set the file length through the file_end_of_file_information structure. The advantage of this method is that you can directly set the length of the Longlong type // /</Summary> file-> length = length; delete file; return true; // reference // http://msdn.microsoft.com/en-us/library/ff567096 (V = vs.85 ). aspx # elseint FD = open (filepath, o_rdwr ); /// <summary> ///-1 if an error occurred // </Summary> If (FD =-1) {return false ;} /// <summary> // on success, zero is returned. on error,-1 is returned, and errno is set appropriately. /// </Summary> return ftruncate (FD, length) = 0; // references // optional}

PS: This method only supports Windows and Linux platforms.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.