std::experimental::filesystem:: resize_file
|
Definido en el encabezado
<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) | |
Cambia el tamaño del archivo regular nombrado por p como si fuera mediante POSIX truncate : si el tamaño del archivo era previamente mayor que new_size , el resto del archivo se descarta. Si el archivo era previamente menor que new_size , el tamaño del archivo se incrementa y la nueva área aparece como si estuviera llena de ceros.
Contenidos |
Parámetros
| p | - | ruta a redimensionar |
| new_size | - | tamaño que tendrá ahora el archivo |
| ec | - | parámetro de salida para reporte de errores en la sobrecarga que no lanza excepciones |
Valor de retorno
(ninguno)
Excepciones
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 hasNotas
En sistemas que admiten archivos dispersos, aumentar el tamaño del archivo no incrementa el espacio que ocupa en el sistema de archivos: la asignación de espacio ocurre únicamente cuando se escriben bytes distintos de cero en el archivo.
Ejemplo
Demuestra el efecto de crear un archivo disperso en el espacio libre.
#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); }
Salida posible:
File size: 1 Free space: 31805444096 File size: 65536 Free space: 31805444096
Véase también
|
devuelve el tamaño de un archivo
(función) |
|
|
determina el espacio libre disponible en el sistema de archivos
(función) |