简体   繁体   English

读取整个标准输入以缓冲

[英]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? 我用getline正确的方法吗? I tried to read it with scanf() , but then I can't copy stdin to buffer. 我试图用scanf()读取它,但是后来我无法将stdin复制到缓冲区。

getline reads until it encounters a new line; getline读取直到遇到新行; hence, it will stop when you press enter the first time. 因此,它将在您第一次按Enter时停止。

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 } . 这样,也将读入新行,并且在读取定界符}之后将停止读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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