Denyk
Denyk

Reputation: 99

Read whole stdin to buffer

i have an input with newlines and i need to read it to buffer. Format is restricted to the structure.

Input looks like this:

{
[
5.5
;
1
]
,   [   1;  2   ]   ,[3; 4]}

And the code I have is like this:

char *s2 = NULL;
size_t n = 0;
int slozZav = 0;
int hranZav = 0;
getline(&s2, &n, stdin);
if(sscanf(s2, " %c [ %lf ; %lf ] , [ %lf ; %lf ] , [ %lf ; %lf ] %c", &s1, &Ax, &Ay, &Bx, &By, &Cx, &Cy, &s) == 8 && s=='}' && s1=='{' && slozZav % 2 == 0 && hranZav % 2 == 0) { ... }

Am I doing it the right way with getline? I tried to read it with scanf(), but then I can't copy stdin to buffer.

Upvotes: 0

Views: 331

Answers (1)

Stephan Lechner
Stephan Lechner

Reputation: 35164

getline reads until it encounters a new line; hence, it will stop when you press enter the first time.

To read in the complete structure to be scanned, try:

getdelim(&s2, &n, '}', stdin);

This way, new lines will be read in as well, and reading will stop after having read the delimiter }.

Upvotes: 2

Related Questions