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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read text without spaces

Status
Not open for further replies.

f90user1

Technical User
Apr 9, 2010
2
0
0
DE
Hi programmers,

I need to read from a text file like:

60.35171 70.94328 81.36008 89.12619
-64.83599-109.01840-139.22970-157.61670
-200.86020-217.46710-230.26000-242.12800
10.05124 61.83500 108.95660 151.30990
113.88240 86.09840 60.00135 43.50320

All floats should go in an array A(i,j).
when there are spaces between each float np, but how does it work without spaces? Any ideas?

 
1) Read the whole line into a char array
2) Find the beginning of a number in the array - not a space
3) Look for the end: either a space or a -
4) Read the number using read(line(numbeg:numend -1), *) A(i,j)
5) Repeat from step 2 until end of line
6) Repeat from step 1 until end of file
 
Or you can first preprocess your input file replacing all '-' with ' -', so you will get from the original file
Code:
 60.35171  70.94328  81.36008  89.12619    
-64.83599-109.01840-139.22970-157.61670
-200.86020-217.46710-230.26000-242.12800
  10.05124  61.83500 108.95660 151.30990 
 113.88240  86.09840  60.00135  43.50320
this file
Code:
60.35171  70.94328  81.36008  89.12619    
 -64.83599 -109.01840 -139.22970 -157.61670
 -200.86020 -217.46710 -230.26000 -242.12800
  10.05124  61.83500 108.95660 151.30990 
 113.88240  86.09840  60.00135  43.50320
and now you can read it from your fortran program
 
Nice one mikrom - good bit of lateral thinking.
 
Thanks for your help guys,
I like the preprocessing idea, very nice!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top