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

How to Add NULL Character to End of CHAR Variable

jkrob
Beginner
3,922 Views
All,

My routine is doing a formatted read of a character string from a file to the character variable CFOO and I need to add a NULL character to the end of that character string within CFOO. How is that done?

Thanks in advance,
Jeff
0 Kudos
2 Replies
anthonyrichards
New Contributor III
3,922 Views

you can generate the NULL character using the function CHAR with zero argument i.e. nullchar=CHAR(0). Add it using the stringconcatenate operator '//' thus

string=TRIM(ADJUSTL(string))//char(0)

ADJUSTL moves the characters in 'string' to the left of the space allotted for the string, removing leading blanks and adding trailing blanks on the right.TRIM chops off all trailing blank characters.You then add theh NULL to the end of the TRImmed result and store the result back into 'string' (or another string). If you know the length of 'string' excluding trailing blanks, you could use

string=string(1:length)//CHAR(0)

This works so long as the space assigned to character string 'string' is at least 1 character longer than 'length'.

0 Kudos
jkrob
Beginner
3,922 Views
Thanks!!! That was it!

Jeff
0 Kudos
Reply