Strumienie wejścioweInput Streams
Obiekt strumienia danych wejściowych jest źródłem bajtów.An input stream object is a source of bytes. Trzy najważniejsze klasy strumienia wejściowego to IStream, ifstreami istringstream —.The three most important input stream classes are istream, ifstream, and istringstream.
istream
Klasa jest Najlepsza do użycia w sekwencyjnym wejściu w trybie tekstowym.The istream
class is best used for sequential text-mode input. Można skonfigurować obiekty klasy istream
dla operacji buforowanej lub niebuforowanej.You can configure objects of class istream
for buffered or unbuffered operation. Wszystkie funkcje klasy podstawowej, ios
,, są zawarte w istream
.All functionality of the base class, ios
, is included in istream
. Obiekty z klasy są rzadko konstruowane istream
.You will rarely construct objects from class istream
. Zamiast tego zwykle używany jest wstępnie zdefiniowany cin
obiekt, który jest obiektem klasy ostream.Instead, you will generally use the predefined cin
object, which is actually an object of class ostream. W niektórych przypadkach można przypisać cin
do innych obiektów strumienia po uruchomieniu programu.In some cases, you can assign cin
to other stream objects after program startup.
ifstream
Klasa obsługuje dane wejściowe pliku dyskowego.The ifstream
class supports disk file input. Jeśli potrzebujesz pliku dyskowego tylko do wprowadzania danych, Utwórz obiekt klasy ifstream
.If you need an input-only disk file, construct an object of class ifstream
. Można określić dane binarne lub w trybie tekstowym.You can specify binary or text-mode data. Jeśli określisz nazwę pliku w konstruktorze, plik zostanie automatycznie otwarty podczas konstruowania obiektu.If you specify a filename in the constructor, the file is automatically opened when the object is constructed. W przeciwnym razie można użyć open
funkcji po wywołaniu konstruktora domyślnego.Otherwise, you can use the open
function after invoking the default constructor. Wiele opcji formatowania i funkcji Członkowskich ma zastosowanie do ifstream
obiektów.Many formatting options and member functions apply to ifstream
objects. Wszystkie funkcje klas podstawowych ios
i istream
są zawarte w ifstream
.All functionality of the base classes ios
and istream
is included in ifstream
.
Podobnie jak w przypadku funkcji biblioteki sscanf_s
, istringstream
Klasa obsługuje dane wejściowe z ciągów w pamięci.Like the library function sscanf_s
, the istringstream
class supports input from in-memory strings. Aby wyodrębnić dane z tablicy znaków, która ma terminator o wartości null, przydzielić i zainicjować ciąg, a następnie utworzyć obiekt klasy istringstream
.To extract data from a character array that has a null terminator, allocate and initialize the string, then construct an object of class istringstream
.
W tej sekcjiIn This Section
Konstruowanie obiektów strumienia danych wejściowychConstructing Input Stream Objects
Korzystanie z operatorów wyodrębnianiaUsing Extraction Operators
Testowanie pod kątem błędów wyodrębnianiaTesting for Extraction Errors
Manipulowanie strumieniami wejściowymiInput Stream Manipulators
Funkcje członkowskie strumienia wejściowegoInput Stream Member Functions
Przeciążanie operatora >> dla własnych klasOverloading the >> Operator for Your Own Classes