Simple formatting question

  • Follow


How do I modify the read/write format statement so that it outputs
exactly what it reads in? If there is a decimal place, it outputs the
decimal place exactly; no decimal means no outputted decimal.

Example #1:
"Input: 123456"

Read (1,'(a6,???)') chrac6, temp_real
Write (*,'(a6,???)') charac6, temp_real

Desired output -
"Input: 123456"

The problem is if I use "Read (1,'(a6,f10.5)')", it will force 5
decimal places onto the figure, as we expect.

Reading "123456" as a character string is possible but undesirable
because I may need to numerically manipulate the figures, which will
mean messy internal write statements.

0
Reply tco99 (2) 6/13/2006 11:24:02 AM

> Reading "123456" as a character string is possible but undesirable
> because I may need to numerically manipulate the figures, which will
> mean messy internal write statements.

That's the only way to do it: Once you have actually performed a formatted
read to a variable, the information on what the input looked like has been
lost. As this is a many-to-one mapping, there's no other way. If you also
need the numerical value, perform an internal read in addition.

	Jan
0
Reply jvorbrueggen1 (362) 6/13/2006 11:29:14 AM


Read the whole line as character, and either tab back to the start of
the numeric field to read just that or, in a separate statement, read
the numeric item from the string that contains the whole line:

      CHARACTER*80 LINE

      READ(1,'(A80,T7,???)')LINE,temp_real
      WRITE(*,'(A,???)')TRIM(LINE),temp_real

      READ(1,'(A80,T7,???)')LINE,temp_real
      READ(LINE,'(6X,???)') temp_real
      WRITE(*,'(A,???)')TRIM(LINE),temp_real

t...@mytrashmail.com wrote:
> How do I modify the read/write format statement so that it outputs
> exactly what it reads in? If there is a decimal place, it outputs the
> decimal place exactly; no decimal means no outputted decimal.
>
> Example #1:
> "Input: 123456"
>
> Read (1,'(a6,???)') chrac6, temp_real
> Write (*,'(a6,???)') charac6, temp_real
>
> Desired output -
> "Input: 123456"
>
> The problem is if I use "Read (1,'(a6,f10.5)')", it will force 5
> decimal places onto the figure, as we expect.
>
> Reading "123456" as a character string is possible but undesirable
> because I may need to numerically manipulate the figures, which will
> mean messy internal write statements.

0
Reply richard.russell (43) 6/13/2006 11:34:42 AM

Dick Russell wrote:

>       CHARACTER*80 LINE
>
>       READ(1,'(A80,T7,???)')LINE,temp_real
>       WRITE(*,'(A,???)')TRIM(LINE),temp_real
>
>       READ(1,'(A80,T7,???)')LINE,temp_real

Ooops! make that just:
      READ(1,'(A80)')LINE

>       READ(LINE,'(6X,???)') temp_real
>       WRITE(*,'(A,???)')TRIM(LINE),temp_real

0
Reply richard.russell (43) 6/13/2006 11:38:24 AM

3 Replies
43 Views

(page loaded in 0.088 seconds)


Reply: