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

How to detect tabulator signs in a character string

reidar
New User
1,369 Views

A program I made gets the data input from user written text files. If the user apply tabs to get the figures in the correct column, the file will be corrupted in the sense that the program is not able to read the data correctly from the file. I try to find a way that the program can scan the input file and remove the tab signs. I tried to use the fortran ICHAR function, but it seems like ICHAR returns the integer value 32, which means blank (bl). How I tested is shown below.

WRITE(IDEDIT,201) ! program prompts

READ(IDEDIT,202)ICOM ! just hit TAb and press return (ICOM is Character*2)

write(idedit,'(I3)' )ichar(icom(1:1)) ! test 22/08-18

write(idedit,'(I3)' )ichar(icom(2:2)) ! test 22/08-18

Hope somebody can give me a clue..

0 Kudos
5 Replies
Arjen_Markus
Honored Contributor I
1,369 Views

You can find a tab character using the index function:

write(*,*) 'Position: ', index( string, achar(9) )

achar is the function to return an ASCII character based on its decimal position in the ASCII table (safer than char, should you encounter a different encoding) and index returns the position of the first matching substring or 0 if there is no such substring.

0 Kudos
mecej4
Honored Contributor III
1,369 Views

The problem is related to the assumption that you stated, "If the user apply tabs to get the figures in the correct column". 

Fortran knows nothing about your notion of "the correct column". The <HT> character (horizontal tab, ASCII 9 or CTRL-I) has no special significance in Fortran, and your text editor (or Excel or whatever program you use to display and edit the input file) may create a file that has no tabs (many editors have an option to replace tabs by an appropriate (?) number of spaces). Opening the same file in another editor may result in a  display of the non-blank characters in different columns than in the first editor that you used.

Either use list-directed input instead of formatted input, or make sure that the input data matches the Format specification used to read the data, and read the sections of the Fortran standard that pertain to list directed and formatted input.

 

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,369 Views

If (when) you have no control over how the user enters data, then it is beneficial to read the user input into a text variable, and then parse the text variable as needed. (IOW you define what TAB or spaces or commas or ?? means)

Jim Dempsey

0 Kudos
reidar
New User
1,369 Views

Thanks you all for your comments. I explained my problem badly, the data are separated by commas, but the user can add blanks to separate data to make the file it more "reader-friendly" to humans.  But when typing the data, the smart guy see that the Tab button is far more effective then the space-bar. The blanks (Char 32) are  removed by the program itself, of course Char 09 can be removed in the same loop, I assume.

0 Kudos
Arjen_Markus
Honored Contributor I
1,369 Views

I have had to deal with a very similar problem the other week - in my case it was spaces and semicolons and the odd slash (/).

Read a line of the file into a string. Sanitize the string by removing the offending characters and then apply list-directed reads. Works as a charm - if you have removed all the garbage. (Including empty fields, that is:two consecutive commas)

0 Kudos
Reply