Reading from standard input using emscripten gets non-existent newline

I am trying to create a C base of code using emscripten and it goes through an abstraction layer for all of its I / O calls. This does not do what I would expect, so I tried a simpler test using here in StackOverflow:

#include <stdio.h> #include <stdlib.h> // From: /questions/63683/how-to-read-a-line-from-the-console-in-c/437628#437628 char * getline_litb(void) { char * line = malloc(100), * linep = line; size_t lenmax = 100, len = lenmax; int c; if(line == NULL) return NULL; for(;;) { c = fgetc(stdin); if(c == EOF) break; if(--len == 0) { len = lenmax; char * linen = realloc(linep, lenmax *= 2); if(linen == NULL) { free(linep); return NULL; } line = linen + (line - linep); linep = linen; } if((*line++ = c) == '\n') break; } *line = '\0'; return linep; } int main() { puts(getline_litb()); return 0; } 

Compiled under gcc or clang this works great. It reads the line until you press enter and return that line. But when I compile it with:

 emcc test.c -o test.bc emcc test.bc -o test.js node test.js 

He thinks a line feed has been entered, so he prints an empty line and does not give me input options. Any ideas?

+4
source share
1 answer

I had a similar problem with this before, but it happened to me after I incorrectly cleared the stdin buffer. Try calling fgetc () once or twice, and then calling it again for real. This may clear some or all of the data that may be left in the standard input buffer. If this dosen't works, I would print f () 'c' on the screen right after it returns from fgetc ().

+1
source

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


All Articles