0

I would like to find a solution for my program to read keyboard input from users for a limited time.

For example, I would like to wait for keyboard input for 5 seconds and after 5 seconds passed, program will continue with or without input (if input is given, it will continue immediately)

I have found some stackoverflow questions asked on C++, however, I couldnt find any solution to solve this problem in fortran.

These are the C++ questions: 1-C++ How to wait for keyboard input for a limited time 2-How to use a timer in C++ to force input within a given time?

Since I dont know fortran very well, I dont prefer to use any other 3rd party libraries, however if you can guide my, I can try them too.

Thank you in advance for your replies.

Community
  • 1
  • 1
  • Which OS are you using? The Fortran language has no features that will help you here - you'll need to use OS libraries to get what you want. – Steve Lionel Jun 22 '16 at 14:59
  • If really necessary, is it not an option to just call the C++ routine in the accepted answer from Fortran? http://stackoverflow.com/questions/28944972/how-to-use-a-timer-in-c-to-force-input-within-a-given-time If it is not good to mix C++ and Fortran I/O systems, it may be better to replace cin and cout with some Fortran routines (I also tried it, but string processing becomes tedious...) – roygvib Jun 22 '16 at 18:04

1 Answers1

2

Fortran is primarily meant for batch numerical computing and as Steve Lionel points out, the language has no features to support this sort of interactive use. Let me make a few suggestions:

Wait indefinitely for user input.

This is the traditional method of getting user input and example code is available near the front of every Fortran textbook. The upside is that it's simple to implement; the downside is that the code effectively hangs until the user responds. Effectively, the code blocks, waiting on user input; there's no signalling or threading system in Fortran to proceed after a time interval has elapsed. On unix systems, I made use of SIGALRM and signal handlers in Perl to implement timeouts but that technique is specific to POSIXy systems and is an example of what can be done with OS libraries, as Steve Lionel noted.

I'm assuming this is the problem you are trying to work around.

Take all user input via an input file and/or command-line switches.

Assume the user knows how your software works and understands how to specify input via the command line or an input file. The Fortran Wiki shows a number of command-line processors which you could use directly. Otherwise, they are good example code for using (modern) Fortran 2003 intrinsics get_command_argument and command_argument_count.

For extended input, you will probably want to use some form of input file. Unfortunately, there's effectively no standard Fortran library which helps with this age-old problem; compare with (for example) Python's ability to read JSON with standard libraries which eliminates the need to invent Yet Another Broken, Proprietary, Unparsable, Garbagey Input File Format (YABPUGIFF). Here is a typical example. Welcome to user interface hell.

In my experience, inventing crappy input file formats and maintaining equally crappy parsers is about 75% of Fortran programming. There are third-party libraries to read and write INI, CSV, JSON, XML, etc. in Fortran. I suggest you investigate those before inventing your own crappy input format.

For programming in general, learn the operating system standard(s) for command line interfaces such as GNU long options and common options such as -v and --verbose for verbose output, -h, --help, and /? for getting help, --usage for showing usage information, etc.

Write the user interface in a different language.

A number of languages interoperate reasonably well with Fortran via the standard module ISO_C_BINDING. I've had good results with Lua, but C and Python work well also.

If you don't need deep access to your code's internals, you can just read input via a wrapper program written in Python, etc., then create the appropriate command line or input file for your Fortran application, then issue a system() or exec() call to actually run your code. I use Python's subprocessor multiprocessing libraries to spawn and watch Fortran executables under both Windows or Linux and have used the Jinja2 template system for generating input for Fortran codes, though any of the popular web-oriented templating frameworks work just as well.

My biggest piece of advice is do not reinvent the wheel; others sorted this out decades ago, follow community standards even though they aren't perfect, and get on with writing interesting code.

arclight
  • 1,608
  • 1
  • 15
  • 19