简体   繁体   中英

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.

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 } .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM