Namespaces
Variants

std::experimental::filesystem:: resize_file

From cppreference.net
ヘッダーで定義 <experimental/filesystem>
void resize_file ( const path & p, std:: uintmax_t new_size ) ;
void resize_file ( const path & p, std:: uintmax_t new_size, error_code & ec ) ;
(filesystem TS)

p で指定された通常ファイルのサイズを、POSIXの truncate と同様に変更します:以前のファイルサイズが new_size より大きかった場合、ファイルの残りの部分は破棄されます。以前のファイルサイズが new_size より小さかった場合、ファイルサイズは増加し、新しい領域はゼロ埋めされたように見えます。

目次

パラメータ

p - リサイズするパス
new_size - ファイルの新しいサイズ
ec - 例外を投げないオーバーロードでのエラー報告用出力パラメータ

戻り値

(なし)

例外

The overload that does not take an error_code & parameter throws filesystem_error on underlying OS API errors, constructed with p as the first argument and the OS error code as the error code argument. std:: bad_alloc may be thrown if memory allocation fails. The overload taking an error_code & parameter sets it to the OS API error code if an OS API call fails, and executes ec. clear ( ) if no errors occur. This overload has
noexcept 仕様:
noexcept

注記

スパースファイルをサポートするシステムでは、ファイルサイズを増やしてもファイルシステム上の占有容量は増加しません:ゼロ以外のバイトがファイルに書き込まれた時のみ、実際の領域割り当てが行われます。

スパースファイルの作成が空き容量に与える影響を実演します。

#include <experimental/filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::experimental::filesystem;
int main()
{
    fs::path p = fs::temp_directory_path() / "example.bin";
    std::ofstream(p).put('a');
    std::cout << "File size:  " << fs::file_size(p) << '\n'
              << "Free space: " << fs::space(p).free << '\n';
    fs::resize_file(p, 64*1024); // resize to 64 KB
    std::cout << "File size:  " << fs::file_size(p) << '\n'
              << "Free space: " << fs::space(p).free << '\n';
    fs::remove(p);
}

出力例:

File size:  1
Free space: 31805444096
File size:  65536
Free space: 31805444096

関連項目

ファイルのサイズを返す
(関数)
ファイルシステムの利用可能な空き容量を判定する
(関数)