Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Limit input characters

stevelong_rrx
초급자
3,504 조회수

I'm looking for a way to limit user keyboard input in a console application that runs in Linux and is compiled with Intel Fortran 2017.

At some juncture, input is collected from the user to provide a comment that is then saved to a database.

The relevant lines of code are:

PRINT *, 'Provide a brief description of your change: '
ACCEPT '(A50)', REVCOM

The REVCOM (revision comment) variable is defined as CHARACTER*50 and it gets inserted into a database field that is defined as VARCHAR(50).

If the user types more than 50 characters, the extra characters are simply discarded (without any "warning" given to the user).  I suppose I could make REVCOM allow more than 50 characters, then if LEN(REVCOM) > 50 tell the user it's too long and make them reenter the comment.  However, I'd rather not make them have to retype anything or count characters.  I'd like to know if there is a way to make the keyboard buffer simply stop accepting more input once 50 characters is reached, so that the user can easily tell when they've reached the maximum length.

0 포인트
1 솔루션
FortranFan
명예로운 기여자 III
3,390 조회수

In @stevelong_rrx 's defense, it was mentioned in the first line of the original post the console app is question runs on Linux.

@stevelong_rrx , I am unaware of an immediate solution you can start using but can make a few long-winded suggestions:

  1. Take a look at the Fortran-lang site (https://fortran-lang.org/) and the resources there under terminal (and color) options, perhaps you might find something useful,
  2. Also, make an inquiry at the related Fortran Discourse site (https://fortran-lang.discourse.group/), perhaps at this thread: https://fortran-lang.discourse.group/t/toml-fortran-0-3-0/4108/2.  Chances are a couple of readers there going by the handles of @awvwgk and @urbanjost may have some options for you.
  3. And/or post an inquiry at the Fortran stdlib site (https://github.com/fortran-lang/stdlib) , especially on the Issues thread at https://github.com/fortran-lang/stdlib/pull/580.  You might receive some useful guidance there.

By the way, you may know `ACCEPT` is a non-standard extension to Fortran - you may consider simply replacing it with the standard READ statement which will be supported reliably by all the Fortran compilers.

Good luck,

 

 

원본 게시물의 솔루션 보기

9 응답
Steve_Lionel
명예로운 기여자 III
3,481 조회수

Fortran has no such feature. If you are on Windows, you could design a form with a text field and that can be limited to 50 characters and do what you want. It's some more coding on your part to design the form, display it and get the values using the IFLOGM (Dialog) library. For more information, see Using Intel® Visual Fortran to Create and Build Windows*-Based Applications

There are also dialog examples in the Samples Bundle.

 

0 포인트
jimdempseyatthecove
명예로운 기여자 III
3,464 조회수

You should be able to, either directly or via a C interop function, call the C Runtime Library function getch and selectively use the CRTL function putch. This should work in both Windows and Linux.

 

Jim Dempsey

stevelong_rrx
초급자
3,383 조회수

Thanks, Jim!  As a Fortran novice and with even less experience with C, I might find pulling this off a little more complicated than the task warrants, but I think I understand the concept (while len(revcom)<50, print the typed character to the screen, append the character to the revcom variable, and accept another character).

0 포인트
andrew_4619
명예로운 기여자 III
3,453 조회수

SetConsoleScreenBufferSize sets the number of lines and the line length of the console buffer. 

 

0 포인트
stevelong_rrx
초급자
3,425 조회수

Is SetConsoleScreenBufferSize part of the Windows API?  I'm not find any documentation that shows how it would be used in a Linux application that runs in a terminal console.

0 포인트
Steve_Lionel
명예로운 기여자 III
3,412 조회수

Yes, that's a Windows API routine. You did not say you were using Linux before.

0 포인트
FortranFan
명예로운 기여자 III
3,391 조회수

In @stevelong_rrx 's defense, it was mentioned in the first line of the original post the console app is question runs on Linux.

@stevelong_rrx , I am unaware of an immediate solution you can start using but can make a few long-winded suggestions:

  1. Take a look at the Fortran-lang site (https://fortran-lang.org/) and the resources there under terminal (and color) options, perhaps you might find something useful,
  2. Also, make an inquiry at the related Fortran Discourse site (https://fortran-lang.discourse.group/), perhaps at this thread: https://fortran-lang.discourse.group/t/toml-fortran-0-3-0/4108/2.  Chances are a couple of readers there going by the handles of @awvwgk and @urbanjost may have some options for you.
  3. And/or post an inquiry at the Fortran stdlib site (https://github.com/fortran-lang/stdlib) , especially on the Issues thread at https://github.com/fortran-lang/stdlib/pull/580.  You might receive some useful guidance there.

By the way, you may know `ACCEPT` is a non-standard extension to Fortran - you may consider simply replacing it with the standard READ statement which will be supported reliably by all the Fortran compilers.

Good luck,

 

 

jimdempseyatthecove
명예로운 기여자 III
3,335 조회수

I suggest that your input routine be constructed to emulate what users are familiar with.

Accept input up to Enter

Handle backspace (rubout)

Echo and insert characters .le. buffer length

Ignore characters in excess of input buffer (permissible to make sound)

If you want to be fancy, permit left and right arrows and character insertion.

 

Note, you should be able to locate (google) a ready made console input routine that does all this.

 

Jim Dempsey

0 포인트
stevelong_rrx
초급자
3,326 조회수

Thanks for the various suggestions!

For now, I've gone with a very low-tech method that isn't foolproof, but was very easy to implement and is better than nothing!

      PRINT *, 'Provide brief description of your change: '
      PRINT *, '(Maximum comment length: 50 characters----------->|'
      ACCEPT '(A50)', REVCOM

This feature won't be used frequently, and if users ignore the on-screen indication of maximum length, and complain about lost characters, then I'll revisit adding a console input routine to the code.

0 포인트
응답