Operating System - OpenVMS
1752318 Members
6270 Online
108786 Solutions
New Discussion юеВ

Re: identify new line character in fortran string which is passed from c++

 
RAVIKANTH_3
Occasional Advisor

identify new line character in fortran string which is passed from c++

I have an application which is written in C++ and fortran.

From C++, i have passed string to fortran which contains new line characters. For example, messages is as shown below
HTTP/1.1 200 OK
Date: Thu, 23 Apr 2009 18:01:14 GMT
Server: Apache/1.3.41 (Unix) PHP/4.0.5

I have complete string message in fortran variable say character*500 str.

Currently i am printing like as shown below
WRITE(*,110)'Response(1-25)',str(1:25)
WRITE(*,110)'Response(26-50)',str(26:50)
WRITE(*,110)'Response(51-75)',str(51:75)
WRITE(*,110)'Response(76-100)',str(76:100)

screen output is:
Response (1-25) : HTTP/1.1 200 OK
Date: Th
Response (26-50) : u, 23 Apr 2009 18:01:01 G
Response (51-75) : MT
Server: Apache/1.3.41
Response (76-100): (Unix) PHP/4.0.5

and I am not getting message output to screen properly, splitted across lines when new line character is there.

Please let me know how to identify new line character("\n") in fortran and print each line seperately.
3 REPLIES 3
Joseph Huber_1
Honored Contributor

Re: identify new line character in fortran string which is passed from c++

Putting aside the question why You want to pass strings from C++ to Fortran just for printing ...

There is no such thing like a "new line character". "\n" is just the C(++) notation for a line terminator, and it is up to the C I/O system to generate whatever the system wants to be the line terminator: In most (all?) Unix-type systems this is Ascii code 10 , on a classic macintosh this is ascii 13 , and in Dos/Windows like systems it represents 2 ascii characters: . (and in multi-byte charactersets it becomes even more complicated).

Now Fortran has no concept of line terminators, it is reading and writing records.
So there is no general solution for Your (pseudo-)problem.
Since the text is apparently coming from a HTTP stream, chances are high the lines are terminated by pairs.

Use the Fortran INDEX intrinsic to search for pairs, then write the found substring without the terminators, and advance to the next part.
Or just search for , ignoring , if the program or routine should be more general.
And watch out for ascii 0 characters, they probably terminate the whole string if it is not really 500 bytes long!

define in Fortran:
character CR,LF,NUL
character*2 CRLF

CR=CHAR(13)
LF=CHAR(10)
NUL=CHAR(0)
CRLF=CHAR(13)//CHAR(10)

and use these as substrings in the INDEX calls.
http://www.mpp.mpg.de/~huber
Joseph Huber_1
Honored Contributor

Re: identify new line character in fortran string which is passed from c++

And
if this not just an exercise in Fortran programming, but writing a HTTP parser (re-inventing the wheel ?), then be aware:

the HTTP header is terminated by TWO consecutive pairs. Anything following is data load, and may not contain "line terminators" at all, or have portions between terminators exceeding the length of records allowed in a write statement (if output is to a 80 or 132 wide terminal for example), or the data is binary and not printable at all !
http://www.mpp.mpg.de/~huber
Richard W Hunt
Valued Contributor

Re: identify new line character in fortran string which is passed from c++

I'll add a little extra to the mix here, maybe.

The issue is that you have mis-divided your labor. The C++ program knows the structure of the input better than FORTRAN, because when you pass in strings, they are either passed via descriptor or fixed-length byte arrays. FORTRAN knows nothing about string structure because STRING is a "cast" of a byte array anyway. So you would scan the array for the terminators in either language, but it might be a bit easier to do it in C++ and just pass in the already-separated lines.

I.e. do parsing of a particular type of record where you receive the record because it is closest to the source of data. The longer you leave something unparsed, the harder it becomes to process it later.

(Just one man's experience in program design speaking here.)
Sr. Systems Janitor