C input function scanf() does not seem to work correctly?

81 views
Skip to first unread message

Marnix

unread,
Jan 2, 2021, 7:52:06 AM1/2/21
to emscripten-discuss

I’m on Windows 10 and have a pretty large (I think) console application written in C. 90+ files and about 17.000 lines of code.

It compiles without errors/warnings in emcc. However, when I run it, the text input does not seem to work correctly. It keeps popping up the input window and buffering text. So I went back to basics and to reproduce and isolate the issue added a scanf() statement to the hello_world program that comes with the emscripten package:


===================================================
/*
* Copyright 2011 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/
#include <stdio.h>

int main() {
  char input[25];

  printf("hello, world!\n");
  printf("Enter input: ");
  scanf("%s", input);
  printf("\nInput is: %s\n", input); 

  return 0;
}
===================================================  

 I compiled the file with command “emcc tests/hello_world.c –o hello_world.html”.

This little program has the same issue. It keeps asking for input and only when I cancel the input window it prints “hello, world!” “Enter input: “ and “Input is: <what I entered>“.

The program is at this link on my website so you can see the behavior: hello_world

What I don't understand: why is the input window coming back each time and why is the text buffered until I cancel the input window?

I did search the group, but actually I don’t know what I’m looking for.

 

Sam Clegg

unread,
Jan 6, 2021, 7:24:41 AM1/6/21
to emscripte...@googlegroups.com
Reading text input via input pop-ups is probably only useful debugging.    I think the primitive support for reading stdin in this way allows you to get things running quickly, but any production application on the web probably needs to be redesigned such that it gets its input some other more web-friendly way.   How do you indent that you users interact with your app?  

If you are interested the code in question (that gets input from `window.input` JS API) seems to be here: https://github.com/emscripten-core/emscripten/blob/289befa9cd7120a7d21835b45903993c44b2c593/src/library_tty.js#L133

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/29395d49-9ada-4100-b542-f1e59602137bn%40googlegroups.com.

Marnix

unread,
Jan 6, 2021, 8:54:08 AM1/6/21
to emscripten-discuss
“How do you indent that you users interact with your app?’

The application is an Interactive Fiction Interpreter. All user input is text (keyboard) and so is the application’s output. When running locally, the application runs ins a CMD-window (windows) or a terminal window (linux and macos). So I think what I need is one web page with an input prompt and output on the same page. Sort of like a chat conversation: enter some text and get a reply.

Op woensdag 6 januari 2021 om 13:24:41 UTC+1 schreef s...@google.com:

Sam Clegg

unread,
Jan 6, 2021, 7:59:40 PM1/6/21
to emscripte...@googlegroups.com
On Wed, Jan 6, 2021 at 5:54 AM Marnix <marni...@gmail.com> wrote:
“How do you indent that you users interact with your app?’

The application is an Interactive Fiction Interpreter. All user input is text (keyboard) and so is the application’s output. When running locally, the application runs ins a CMD-window (windows) or a terminal window (linux and macos). So I think what I need is one web page with an input prompt and output on the same page. Sort of like a chat conversation: enter some text and get a reply.

In that case maybe you want to look into some kind of web-based terminal similar UI.. you can then hook that up to emscripten either via stdin/stdout, or probably more simply via direct functions call from JS to C++ and back again.
For terminals in JS it looks like there are many options: https://xtermjs.org/ https://hterm.org/ both look good.

You probably don't want to be doing `window.input` which is why emscripten is doing under the hood by default here.   

Having said that there may be a bug that is worth fixing around how stdin is being read from the result of `window.input`.. its might be worth fixing but it doesn't sounds like it would lead you to a solution you could use in production.

Marnix

unread,
Jan 7, 2021, 3:12:36 PM1/7/21
to emscripten-discuss
I checked my hello_world.js and the github code with window.prompt you pointed to in your first reply is also in my hello_world.js. I also found that the output is done through the out() function in the js.

So I must replace windows.prompt with something else that not generates a popup window but takes output from a simulated terminal window? Ands I must make out() print to that same window? So. I'm going to have a look at the terminal window simulators for websites you posted.



Reply all
Reply to author
Forward
0 new messages