4

In my ATS application, I am trying to read a input string from a user. Is there any function in ATS that performs similar functionality as scanf function in C.. If not how to get the input from user without integrating ATS with JS or HTML.

Himanir
  • 41
  • 1
  • 4

2 Answers2

2

Here is a simple way to read from STDIN:

#include
"share/atspre_staload.hats"
#include
"share/HATS/atspre_staload_libats_ML.hats"

implement
main0() =
{
//
val
lines =
streamize_fileref_line(stdin_ref)
//
val () = lines.foreach()(lam x => println! (x))
//
} (* end of [main0] *)
Hongwei Xi
  • 895
  • 1
  • 5
  • 10
  • how is this compiled? `patscc -DATS_MEMALLOC_LIBC -latslib file.dats -o file` gives me an undefined reference to `atspre_fileref_get_line_string_main2` – Julian Fondren Jan 04 '18 at 04:17
0

If you compile to C, then scanf is available. Here is a simple example:

#include
"share/atspre_staload.hats"
#staload
"libats/libc/SATS/stdio.sats"

implement
main0() =
{
//
var str1 = @[char][1024]()
var str2 = @[char][1024]()
//
val () = println! ("Enter name: ")
val ec = $extfcall(int, "scanf", "%s", addr@str1)
val () = assertloc (ec != 0)
val str1 = $UNSAFE.cast{string}(addr@str1)
//
val () = println! ("Enter your website name: ")
val ec = $extfcall(int, "scanf", "%s", addr@str2)
val () = assertloc (ec != 0)
val str2 = $UNSAFE.cast{string}(addr@str2)
//
val () = println! ("str1 = ", str1)
val () = println! ("str2 = ", str2)
//
}
Hongwei Xi
  • 895
  • 1
  • 5
  • 10