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

Character Count Editing (Q)

rogcar
Beginner
1,365 Views
Hello there,

I tried to use the Q character count in a format sentence, and it's giving a runtime input conversion error. The error alsohappens withthe example given in the help. I'm using v11.1.046. The following example was extracted from the help:

Assume the file Q.DAT contains:

1234.567Hello

The following program reads in the number REAL1, determines the characters left in the record, and reads those into STR:

CHARACTER STR(80)
INTEGER LENGTH
REAL REAL1
OPEN (UNIT = 10, FILE = 'Q.DAT')
100 FORMAT (F8.3, Q, 80A1)
READ (10, 100) REAL1, LENGTH, (STR(I), I=1, LENGTH)
PRINT * , str
END

Any guess?
Regards,
Roger
0 Kudos
11 Replies
jimdempseyatthecove
Honored Contributor III
1,365 Views

Roger,

What happens when you read just the F8.3 REAL1 (using same file, but different FORMAT and READ)?
If no error, what is the value of REAL1?
or
If this produces conversion error, then read line into STR using 80A1. Is there junk in the line?

Jim Dempsey
0 Kudos
rogcar
Beginner
1,365 Views

Hello Jim

If I read just the REAL1, using F8.3, it gives the correct value: 1234.567. If I use something different (like F9.3), it will give an error, since the number has 8 chars. I was trying to find the error here, but I realize that it is not a Q problem, but a format problem. I'm still trying and now I could get this example to work. It was an typing error (I wrote 1234,567 <- notice the comma).

Nevertheless, I still trying to use the format thing in the read statement, and having some problems. I'll try to explain:
Lets say that I need to read the following three lines:
123 testing line 1
12 testing line 2
1 testing line 3

How can I read the first number (an integer) and the rest put into a char? If I use a FORMAT, like "10 FORMAT (I3,80A1)" and then read the text, the third line will give me an error (since the read integer will be "1 t"). If I use a unformatted read (*), I get the correct number, but the string gives only "testing". Do you know any workaround?

I hope I was able to explain...
Thanks for your kind help
Roger
0 Kudos
rogcar
Beginner
1,365 Views
I found the answer. Two rules apply: If I use a formated input, it must have the correct size. That means I cannot use the formated read. The second rules says that,if using the unformated read, whenever a blank character is found, the character string read is stoped.

Well, the only work around is to create a function to determine the value of the size using a formated input, but that would slow down the reading process.

If someone knowsother way, I'd appreciate your kind suggestion.

Best regards
Roger
0 Kudos
Steven_L_Intel1
Employee
1,365 Views
Why are you using 80A1 rather than A (or A80)? 80A1 isn't what you want when reading a CHARACTER(80) variable.
0 Kudos
rogcar
Beginner
1,365 Views
Why are you using 80A1 rather than A (or A80)? 80A1 isn't what you want when reading a CHARACTER(80) variable.

Actually, I using an example given by the help files. Notice that it is CHARACTER(80), instead of CHARACTER*80. The first is an array of chars, the second is a string. It may seen silly, but they are different. If you declare a CHARACTER(2)*80 you are declaring an array of 2 string, each with 80 positions. But if you declare CHARACTER(80)*2 you are declaring 80 strings 2 character long. So, if you use 80A1, the format specifies 80 characters, but if you use A80, it is a string 80 characters long.

Weird, hum! Anyway, it was just a copy and paste :). Ow, in the example, it is 80A1, as the variable was declared as CHARACTER(80). Nevertheless, I still have to read unformated text in a formated fashion... Any ideas?

Best regards
Roger
0 Kudos
Steven_L_Intel1
Employee
1,365 Views

CHARACTER(80) and CHARACTER*80 have the same meaning.
0 Kudos
Paul_Curtis
Valued Contributor I
1,365 Views
Quoting - rogcar
123 testing line 1
12 testing line 2
1 testing line 3

How can I read the first number (an integer) and the rest put into a char?

[cpp]INTEGER  :: ival, nc
CHARACTER(LEN=40) :: string

OPEN (3, FILE=somefile, STATUS='OLD')
READ (3, '(A)') string

! first blank delimits integer field
nc = INDEX(string, ' ') - 1
IF (nc >= 1) THEN
READ (string(1:nc), '(I)') ival

! left-pack the rest of the string
string = string(nc+2:)

! input error
ELSE
!
END IF[/cpp]

0 Kudos
rogcar
Beginner
1,365 Views
Thank you, Paul Curtis. This piece of code will work!

And Steve, although they may have the same meaning, theyare different. Notice that if you compile the same example changing only CHARACTER(80) to CHARACTER*80, it won't work. Besides,put a new variable in the test program and debug it. You will notice that one is an array of chars, the other is a string. Again, they may seen the same, yet they are different.

Regards,
Roger
0 Kudos
Steven_L_Intel1
Employee
1,365 Views

What you have in the initial example is:

CHARACTER STR(80)

That is indeed an array of 80 single characters. What I referred to would be:

CHARACTER(80) STR

I apologize for not making this clear.
0 Kudos
rogcar
Beginner
1,365 Views
Ow, I see. Now I realize that I also haven't been clear myself. I also apologize, for Idid not understand what you said before. Putting that way,I do agree with you.By the way, after re-reading my last post,I may have sound rude, butI didn't want to. That was not what I meant. Sorry.

Regards,
Roger
0 Kudos
Steven_L_Intel1
Employee
1,365 Views
I didn't think you were rude.

Looking at the original example, I can see it is confusing. It is almost prehistoric, I think, but it does illustrate the feature. Maybe I will be able to think of a better way to show it off.
0 Kudos
Reply