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 보다 작은 경우 파일 크기가 증가하며 새로운 영역은 0으로 채워진 것처럼 나타납니다.

목차

매개변수

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

참고 사항

희소 파일을 지원하는 시스템에서는 파일 크기를 늘려도 파일 시스템에서 차지하는 공간이 증가하지 않습니다: 0이 아닌 바이트가 파일에 기록될 때에만 공간 할당이 이루어집니다.

예제

스파스 파일 생성이 여유 공간에 미치는 영향을 보여줍니다.

#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

참고 항목

파일의 크기를 반환합니다
(함수)
파일 시스템의 사용 가능한 여유 공간을 확인합니다
(함수)