Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output formatting 2

Status
Not open for further replies.

riskeevo

Programmer
Oct 1, 2012
6
BG
Hello, guys. I started with Fortran one week ago and came to the following problem...
Consider, I have one file named "r1.i" with this data inside: (assume that the underscore (_) is blank space)
TEXTTOBEREMOVED_MORETEXT_111_121_131
TEXTTOBEREMOVED_MORETEXT_112_122_132
TEXTTOBEREMOVED_MORETEXT_113_123_133
TEXTTOBEREMOVED_MORETEXT_114_124_134

I need to take the numbers at the end and copy them to another file, which I named "r2.i", so they will appear inside like this:
111_121_131
112_122_132
113_123_133
114_124_134
Here is my code:
Code:
	program array 
	implicit none	
	character t1,t2
	integer i,n
	integer, allocatable :: x(:),y(:),z(:)

	! FILES
	open(1,file='r1.i',status='old')
	open(2,file='r2.i',status='replace')

	! ARRAY
	allocate(x(n),y(n),z(n))
	n=1000
	do i=1,n
		read (1,*,end=2) t1,t2,x(i),y(i),z(i)
		write (2,*)x(i),y(i),z(i)
		print *,x(i),y(i),z(i)
	enddo 
2	continue
	close(2)
	close(1)
	end program array

When I run this program, it is doing its job, but it gives me the array with this format:
_________111_________121_________131
_________112_________122_________132
_________113_________123_________133
_________114_________124_________134
I need to remove those spaces, so I make "format (I3,1X,I3,1X,I3)" labeled 3. When I put the label 3 into "write (2,3)x(i),y(i),z(i)" and "print 3,x(i),y(i),z(i)", the array.exe prompts this row:
___________4_________121_________131
and it says: "array.exe has encountered a problem and needs to close. We are sorry for the inconvenience." etc.

Please, can you help me? How am I supposed to solve this error?
Thanks in advance!
 
Why you have used n before initialization?
You allocate arrays with undefined n.
Of course, your program crashed after that...
 
Hi riskeevo,

As ArkM said, the code you posted doesn't work. But for this task you don't need the arrays. If your TEXTTOBEREMOVED_MORETEXT doesn't contain digits you can do something like this:

Code:
[COLOR=#a020f0]program[/color] riskeevo
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#2e8b57][b]character[/b][/color][COLOR=#804040][b]*[/b][/color][COLOR=#ff00ff]80[/color] :: line

  [COLOR=#2e8b57][b]integer[/b][/color] :: pos, x, y, z

  [COLOR=#0000ff]! FILES[/color]
  [COLOR=#804040][b]open[/b][/color]([COLOR=#ff00ff]1[/color],[COLOR=#804040][b]file[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'r1.i'[/color],[COLOR=#804040][b]status[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'old'[/color])
  [COLOR=#804040][b]open[/b][/color]([COLOR=#ff00ff]2[/color],[COLOR=#804040][b]file[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'r2.i'[/color],[COLOR=#804040][b]status[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'replace'[/color])

  [COLOR=#804040][b]do[/b][/color] [COLOR=#804040][b]while[/b][/color] ([COLOR=#ff00ff].true.[/color])
    [COLOR=#0000ff]! read line[/color]
    [COLOR=#804040][b]read[/b][/color] ([COLOR=#ff00ff]1[/color],[COLOR=#ff00ff]'(A)'[/color],[COLOR=#804040][b]end[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]99[/b][/color]) line
    [COLOR=#0000ff]! scan for first position where number begins[/color]
    pos [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]scan[/color](line,[COLOR=#ff00ff]'0123456789'[/color])    
    [COLOR=#0000ff]! when numbers is found, then extract numbers[/color]
    [COLOR=#804040][b]if[/b][/color] (pos [COLOR=#804040][b]>[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
      [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'line found: "'[/color], [COLOR=#008080]adjustl[/color]([COLOR=#008080]trim[/color](line)), [COLOR=#ff00ff]'"'[/color] 
      [COLOR=#804040][b]read[/b][/color](line(pos:),[COLOR=#804040][b]*[/b][/color]) x, y, z
      [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'extracted numbers: x='[/color], x, [COLOR=#ff00ff]'y='[/color] , y ,[COLOR=#ff00ff]'z='[/color], z
      [COLOR=#0000ff]! write to file[/color]
      [COLOR=#804040][b]write[/b][/color] ([COLOR=#ff00ff]2[/color],[COLOR=#ff00ff]'(I3,1x,I3,1x,I3)'[/color]) x, y, z
    [COLOR=#804040][b]end if[/b][/color]
  [COLOR=#804040][b]enddo[/b][/color] 
  [COLOR=#6a5acd]99[/color] [COLOR=#804040][b]continue[/b][/color]
  [COLOR=#804040][b]close[/b][/color]([COLOR=#ff00ff]2[/color])
  [COLOR=#804040][b]close[/b][/color]([COLOR=#ff00ff]1[/color])
[COLOR=#a020f0]end program[/color] riskeevo

For this input file r1.i (I modified it a little bit, so that it isn't so trivial:))
Code:
FIRST TEXTTOBEREMOVED MORETEXT 111 121 131 
 SECOND TEXTTOBEREMOVED  MORETEXT 112   122   132
  THIRD TEXTTOBEREMOVED, THEN  MORETEXT 113 123   133
   FOURTH TEXTTOBEREMOVED AND MORETEXT         114  124  134
the code above produces this output file r2.i
Code:
111 121 131
112 122 132
113 123 133
114 124 134
 
First to thank you both, guys!
ArkM, I moved the definition of n before the allocating and its working also with the formatting just like I need, but why didn't it indicate an exact error and why it was working without the formatting? I'm confused.

Mikrom, my TEXTTOBEREMOVED_MORETEXT was a sample just to try the output formatting. The real input file has a specific mixed data, from which I need the last two numbers from each row starting with ENODE:

Code:
BEAMVECTOR /  3  0  0  1
ENODE A=G797 /  250002 -300 178928 178831
EDATA / ENTRIES EL SAVE /   250002 Yes

BEAMVECTOR /  4  0  0  1
ENODE A=G797 /  250003 -4000 178831 178734
EDATA / ENTRIES EL SAVE /   250003 Yes

BEAMVECTOR /  5  0  0  1
ENODE A=G797 /  250004 -50 178734 178637
EDATA / ENTRIES EL SAVE /   250004 Yes

, so my output has to be:
Code:
178928 178831
178831 178734
178734 178637

Your code for program riskeevo gives me an "error FOR1127: internal compiler error / Command line error D2013" which points the line with:
Code:
      write(*,*) 'line found: "', adjustl(trim(line)), '"'
I must say I am on Fortran 90.
Many thanks again,
R
 
riskeevo,
The osurce I posted above will not work for your current data.
 
mikrom,
I know, I ran your code with your data for the input file and it gave me the error mentioned.
 
Here is the modified source

Code:
[COLOR=#a020f0]program[/color] riskeevo
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#2e8b57][b]character[/b][/color][COLOR=#804040][b]*[/b][/color][COLOR=#ff00ff]80[/color] :: line

  [COLOR=#2e8b57][b]integer[/b][/color] :: pos, x, y, z, t

  [COLOR=#0000ff]! FILES[/color]
  [COLOR=#804040][b]open[/b][/color]([COLOR=#ff00ff]1[/color],[COLOR=#804040][b]file[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'riskeevo_inp.txt'[/color],[COLOR=#804040][b]status[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'old'[/color])
  [COLOR=#804040][b]open[/b][/color]([COLOR=#ff00ff]2[/color],[COLOR=#804040][b]file[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'riskeevo_out.txt'[/color],[COLOR=#804040][b]status[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'replace'[/color])

  [COLOR=#804040][b]do[/b][/color] [COLOR=#804040][b]while[/b][/color] ([COLOR=#ff00ff].true.[/color])
    [COLOR=#0000ff]! read line[/color]
    [COLOR=#804040][b]read[/b][/color] ([COLOR=#ff00ff]1[/color],[COLOR=#ff00ff]'(A)'[/color],[COLOR=#804040][b]end[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]99[/b][/color]) line
    line [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]adjustl[/color]([COLOR=#008080]trim[/color](line))
    [COLOR=#0000ff]! if pattern found [/color]
    [COLOR=#804040][b]if[/b][/color] (line([COLOR=#ff00ff]1[/color]:[COLOR=#ff00ff]5[/color]) [COLOR=#804040][b].eq.[/b][/color] [COLOR=#ff00ff]'ENODE'[/color]) [COLOR=#804040][b]then[/b][/color] 
      [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'line found: "'[/color], [COLOR=#008080]adjustl[/color]([COLOR=#008080]trim[/color](line)), [COLOR=#ff00ff]'"'[/color]
      pos [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]index[/color](line,[COLOR=#ff00ff]'/'[/color])
      [COLOR=#804040][b]read[/b][/color](line(pos[COLOR=#804040][b]+[/b][/color][COLOR=#ff00ff]1[/color]:),[COLOR=#804040][b]*[/b][/color]) x, y, z, t
      [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'extracted numbers: x='[/color], x, [COLOR=#ff00ff]'y='[/color] , y ,[COLOR=#ff00ff]'z='[/color], z, [COLOR=#ff00ff]'=t='[/color], t
      [COLOR=#0000ff]! write to file last 2 numbers[/color]
      [COLOR=#804040][b]write[/b][/color] ([COLOR=#ff00ff]2[/color],[COLOR=#ff00ff]'(I6,1x,I6)'[/color]) z, t
    [COLOR=#804040][b]end if[/b][/color]
  [COLOR=#804040][b]enddo[/b][/color] 
[COLOR=#804040][b]  99 continue[/b][/color]
  [COLOR=#804040][b]close[/b][/color]([COLOR=#ff00ff]2[/color])
  [COLOR=#804040][b]close[/b][/color]([COLOR=#ff00ff]1[/color])
[COLOR=#a020f0]end program[/color] riskeevo

Outpput:
1. Console
Code:
$ g95 riskeevo.f95 -o riskeevo

$ riskeevo
 line found: "ENODE A=G797 /  250002 -300 178928 178831"
 extracted numbers: x= 250002 y= -300 z= 178928 =t= 178831
 line found: "ENODE A=G797 /  250003 -4000 178831 178734"
 extracted numbers: x= 250003 y= -4000 z= 178831 =t= 178734
 line found: "ENODE A=G797 /  250004 -50 178734 178637"
 extracted numbers: x= 250004 y= -50 z= 178734 =t= 178637
2. File:
Code:
178928 178831
178831 178734
178734 178637
 
riskeevo said:
I know, I ran your code with your data for the input file and it gave me the error mentioned.
What compiler are you using? What I posted works with g95 and gfortran
 
I tried again both of your codes, it gives me the same error. I will try to modify them for the older version and I will come back here. Thank you!
 
Fortran 90" is language standard, I asked for the name of your compiler.
Maybe your compiler doesn't support one of the intrinsic functions adjustl(), trim() or both
try to modify the line
Code:
line = adjustl(trim(line))
or delete it and look if it works
 
Out of curiousity I coded it without looking at the previous answers and... ...ended up with something like Mikrom's code

Code:
PROGRAM GetNumbers
IMPLICIT NONE
INTEGER :: i
INTEGER, DIMENSION(4) :: node
CHARACTER(LEN=80) :: Line

OPEN(UNIT=11,FILE='input.txt',STATUS='OLD')
OPEN(UNIT=12,FILE='output.txt',STATUS='REPLACE')
DO WHILE(.TRUE.)
   READ(11,'(A80)',ERR=100,END=100)Line
   IF(INDEX(Line,'ENODE')/=0)THEN
      READ(Line(INDEX(Line,'/')+1:80),*)(node(i),i=1,4,1)
      WRITE(12,*)(node(i),i=3,4,1)
      CALL FLUSH(12)
   END IF
END DO
100 CONTINUE
CLOSE(11,STATUS='KEEP')
CLOSE(12,STATUS='KEEP')

END PROGRAM GetNumbers

However, this was after a second try, because I wanted to do:

Code:
PROGRAM GetNumbers
IMPLICIT NONE
INTEGER :: i1,i2
CHARACTER(LEN=1) :: ch
CHARACTER(LEN=80) :: Line

OPEN(UNIT=11,FILE='input.txt',STATUS='OLD')
OPEN(UNIT=12,FILE='output.txt',STATUS='REPLACE')
DO WHILE(.TRUE.)
   READ(11,'(A80)',ERR=100,END=100)Line
   IF(INDEX(Line,'ENODE')/=0)THEN
      READ(Line,*)ch,ch,ch,ch,ch,ch,ch,ch,i1,i2
      WRITE(12,*)i1,i2
      CALL FLUSH(12)
   END IF
END DO
100 CONTINUE
CLOSE(11,STATUS='KEEP')
CLOSE(12,STATUS='KEEP')

END PROGRAM GetNumbers

But for some vague reason Fortran doesn't read "/" as a word... anyway Mikrom's solution works in gfortran.
(replacing / by RR or so, the second code will work as well)
 
Sorry for my delay, guys.
Mikrom, I read about these two functions, adjustl() and trim(), they are unsupported in earlier versions than Fortran95.
I deleted the lines from your codes, both codes extract the numbers needed to the output files, only the console printout changes, but it is easy to format it.
GerritGroot, both of your codes are working perfect. I made it first time by replacing the "'", but I wanted to know how it could be done without, so I asked here.
Thank you all, answers were really helpful!
R
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top