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

Equivalent of the C++ \n with fortran

Ines_F_
Beginner
1,527 Views

I'm trying to read an ASCII file with MPI_FILE_READ. I found out that I need first of all to read the entire file and put it on a character string.

The problem is that I didn't found how to split this string on a lines because I don't know how to detect a new line in a character string with fortran.

In some way I'm searching the equivalent of this C++ code in Fortran :

nlines = 0;
for (int i=0; i<file_size;i++)
     if (data == '\n') (*nlines)++;

 

Thanks,

Ines

0 Kudos
3 Replies
andrew_4619
Honored Contributor III
1,527 Views

You can use the char function char(10) is linefeed LF and char(13) is carriage return CR

0 Kudos
Ines_F_
Beginner
1,527 Views

Thanks a lot for the answer. It works with CHAR(10).  

0 Kudos
IanH
Honored Contributor III
1,527 Views

If you are a little paranoid, ACHAR(10) is better - this is a little more robust to the default character kind not being ascii.  The NEW_LINE standard intrinsic is even more robust.

There's also the C_NEW_LINE and C_CARRIAGE_RETURN constants from the ISO_C_BINDING module for kind C_CHAR characters.

0 Kudos
Reply