Espacios de nombres
Variantes
Acciones

std::fread

De cppreference.com
< cpp‎ | io‎ | c
 
 
 
 
Definido en el archivo de encabezado <cstdio>
std::size_t fread(void* buffer, std::size_t size, std::size_t count, std::FILE* stream );
Lee hasta objetos count en el buffer matriz a partir de la corriente de entrada dado stream como si llamando std::fgetcsize veces para cada objeto, y el almacenamiento de los resultados, en el orden obtenido en las sucesivas posiciones de buffer, que se reinterpreta como una matriz de unsignedchar. El indicador de posición de fichero para el flujo se hace avanzar por el número de caracteres leídos .
Original:
Reads up to count objects into the array buffer from the given input stream stream as if by calling std::fgetcsize times for each object, and storing the results, in the order obtained, into the successive positions of buffer, which is reinterpreted as an array of unsignedchar. The file position indicator for the stream is advanced by the number of characters read.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Si los objetos no son TriviallyCopyable, el comportamiento no está definido .
Original:
If the objects are not TriviallyCopyable, the behavior is undefined.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Si se produce un error, el valor resultante del indicador de posición del fichero para el flujo es
Original:
If an error occurs, the resulting value of the file position indicator for the stream is
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
indeterminada. Si un elemento parcial es leído, su valor es indeterminado
Original:
indeterminate. If a partial element is read, its value is indeterminate
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Contenido

[editar]Parámetros

buffer -
puntero hacia el objeto primero de la matriz para ser leído
Original:
pointer to the first object in the array to be read
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
size -
tamaño de cada objeto en bytes
Original:
size of each object in bytes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
count -
el número de los objetos que se deben leer
Original:
the number of the objects to be read
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[editar]Valor de retorno

Número de objetos leídos con éxito, lo que puede ser inferior a count si una condición de error o de fin de archivo se produce .
Original:
Number of objects read successfully, which may be less than count if an error or end-of-file condition occurs.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Si size o count es cero, fread devuelve cero y no realiza ninguna otra acción .
Original:
If size or count is zero, fread returns zero and performs no other action.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[editar]Ejemplo

#include <iostream>#include <cstdio>#include <fstream>#include <vector>int main(){// prepare filestd::ofstream("test.txt")<<1<<' '<<2<<'\n';std::FILE* f =std::fopen("test.txt", "r");   std::vector<char> buf(4);// char is trivally copyable std::fread(&buf[0], sizeof buf[0], buf.size(), f);   for(char n : buf)std::cout<< n;   std::vector<std::string> buf2;// string is not trivially copyable// this would result in undefined behavior// std::fread(&buf2[0], sizeof buf2[0], buf2.size(), f);}

Salida:

1 2

[editar]Ver también

lee la entrada con formato desde stdin, una secuencia de archivo o un tampón
Original:
reads formatted input from stdin, a file stream or a buffer
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(función)[editar]
recibe una cadena de caracteres a partir de una secuencia de archivo
Original:
gets a character string from a file stream
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(función)[editar]
escribe en un archivo
Original:
writes to a file
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(función)[editar]
close