How to set the maximum read length for a stream in C ++?

I am reading data from a stream into a char array of a given length, and I would like the maximum reading width to be large enough to fit into this char array.

The reason I use a char array is because part of my specification is that the length of any single token cannot exceed a certain value, so I save some constructor calls.

I thought width () did what I wanted, but I was clearly wrong ...

EDIT: I use stream extraction operators to perform extraction, as these are text text files with values ​​separated by spaces.

+3
source share
2
char x[4];
cin.width(4);
cin >> x;
cout << x;

: "abcdef"
: "abc" (x[3] char)

.

. , cin.width . , cin >> setw(4) >> x;, iomanip.

+3

, get: http://cppreference.com/wiki/io/get

const int size = 200;
char myArray[size] = {};

cin.get(myArray, size);

: size - 1, NULL- myArray.

, , , read: http://cppreference.com/wiki/io/read

const int size = 200;
char myArray[size] = {};

cin.read(myArray, size);

size .

+5

Source: https://habr.com/ru/post/1737550/


All Articles