WRITE and C Strings

WRITE and C Strings

This is bugging me, as I'm porting a DOS program which returns strings from C to FORTRAN.

If I WRITE a C string either made by copying a C character constant or by using one returned from C, then whenever the NULL character is hit, a carriage return/line feed seems to be thrown.

I don't think this is reasonable - I would have thought it more reasonable to output a space?

I have to have an extra function to manipulate my strings to strip the nulls from them before writing them - which is a pain.

If anyone has another solution let me know.

The following illustrates it. compiled with 9.1

program ftest

character*20 longtext
character*12 shorttext

longtext='1234567890123456789' ! ie 19 characters
shorttext='abcdefghijkl' ! 12 characters

write(*,*)'Case 1 - bog standard Fortran strings'
write (*,20) longtext,shorttext
20 format('Long Text : ',A20,' Short Text : ',A12)

write(*,*)'Case 2 - copy a 19 char C string'
longtext='1234567890123456789'C ! ie 19 characters
write (*,20) longtext,shorttext

write(*,*)'Case 3 - print only 19 characters of the C string'
write (*,20) longtext(:19),shorttext

stop
end

which produces

Case 1 - bog standard Fortran strings
Long Text : 1234567890123456789 Short Text : abcdefghijkl
Case 2 - copy a 19 char C string
Long Text : 1234567890123456789
Short Text : abcdefghijkl
Case 3 - print only 19 characters of the C string
Long Text : 1234567890123456789 Short Text : abcdefghijkl

3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Curious. When I try your program with 9.1.025, I get:

Case 1 - bog standard Fortran strings
Long Text : 1234567890123456789 Short Text : abcdefghijkl
Case 2 - copy a 19 char C string
Long Text : 1234567890123456789 Short Text : abcdefghijkl
Case 3 - print only 19 characters of the C string
Long Text : 1234567890123456789 Short Text : abcdefghijkl

which is what I think you want.

Steve - Intel Developer Support

doh!

I originally had some write loops in the program & I was running
ftest | more
but just F3'd the command even after taking the write loops out.
It's more.com thats putting the carriage returns.

I've since found the bug in my original program which was down to a duff fortran strlen() routine.

Sorry for the trouble

Leave a Comment

Please sign in to add a comment. Not a member? Join today