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

How to use advance='no' feature while reading a file

Status
Not open for further replies.

nickjw

Programmer
May 2, 2012
5
CA
I have large files with 5 columns of numbers. First column is an integer and other columns are reals, with one space in between all columns. i want to be able to read the first two numbers on a line, but be able to read the others later without having to backspace. The advance='no' feature is not working for me, as it requires specifying the format of the variables being read, but the format changes randomly.

The main issue is that there are negative as well as positive numbers in my files, and the negative numbers take up one more character space than the positive numbers (if only the positive numbers came with a "+"!). There is no way to predict when a number will be negative, and so i can't come up with a way to specify the format that will work for numbers which have fluctuating lengths. Any ideas are greatly appreciated. i am considering regenerating these files with no negative numbers, but this would be a lengthy process!
 
Addendum:

The column of integers is now the problem. I could indeed recreate these files so that i have no negative numbers, but i just realized that the column of integers includes numbers up to 1000. So now the problem is how do i specify format for integers which vary from 1 to 3 characters
 
Will you please post a few lines of the data in question.
And,
Will you please post the read statement that you are using, etc.?
And,
will you please elaborate why it is that you cannot read 3 extra numbers at that time...o.k., I will admit, I am just curious about this one and like playing devil's advocate.

 
Instead of using advance='no', you could read the line into a character buffer then read the character buffer into your array. Something like this
Code:
character(80):: line, fmt
...
! Open file on channel 100
...
! Read a line from channel 100
read (100, '(A)') line
! Work out the format in fmt
...
! Read the data
read (line, fmt) (numval(ii), ii = 1, n)

If the numbers never join up, then you don't need the format bit: you can just use *. You only need to specify the format if they join up. Normally, these are fixed format fields so as long as you've worked out what the format is for the first line, the rest of the lines fall into place. For instance,
Code:
   1   2   3  -> should get 1, 2, 3
 2345678  90  -> should get 234, 5678, 90
  22   91234  -> should get 22, 9, 1234
 
Thanks for the help! I should have included the data, don't know why i didnt. and actually the integers can have any amount of characters as far as i can tell. Here is a short example:

0 -0.123456e-01 1.123456e-03 -2.123456e+00 3.123456e-01
1 -4.123456e-01 5.123456e-03 -6.123456e+00 7.123456e-01
17745 -8.123456e-01 -9.123456e-03 -6.123456e+00 7.123456e-01
32 -0.123456e-01 1.123456e-03 -2.123456e+00 3.123456e-01


I read the suggested related thread, and the only option which would work i believe is the pre sorting of the data with a sed command or some such maneuver. However my files can be in the tens of GB, and dont have much experience with command line stuff, so i don't know if this is possible with such large files?

what im trying to do is something like:

do i = 1, n

read(10, ?...) a, b
if(something) then
read(10, ?...) c
if(something else)then
read(10, ?) d, e

end do

where a,b,c,d,e are all from the same line. It would work if you could use the advance=no with a * format, but alas its not to be. As well, there is nothing delimiting the columns which may make it more difficult?

There are plenty of options to do this im sure, but i require a solution which is fast, as i can already accomplish the task by reading the whole line every time and backspacing.

xwb, i am unfamiliar with your method, but i will look into it now, thanks!
 
{code]
open (unit = 10, file = 'input.txt')

do
read (10, *, iostat = irslt) i1, a1, a2, a3, a4
if (irslt .ne. 0) exit
write (*,*) i1, a1, a2, a3, a4
enddo
stop
end
[/code]

this code read your sample data perfectly.


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Had to figure a way to copy the printout. Here is what my code produced:
Code:
           0 -1.2345600E-02  1.1234561E-03  -2.123456      0.3123456    
           1 -0.4123456      5.1234560E-03  -6.123456      0.7123456    
       17745 -0.8123456     -9.1234557E-03  -6.123456      0.7123456    
          32 -1.2345600E-02  1.1234561E-03  -2.123456      0.3123456

Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
hah, oh yes this is a good idea, just make another program to modify the input data. thanks gummibaer! i also realized the command
column -t file.txt
does the same thing (kinda). Thanks again
 
No, you got me wrong. No additional program or preprocessing or messing around with formats.

This is the read statement that solves your problem:

Code:
read (10, *, iostat = irslt) i1, a1, a2, a3, a4

Only this and nothing more. i1 is an integer, a1 to a4 are reals.
Of course you may want to use the names of the variables in your program, but you do not need any more.

Norbert

The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
i cant use that to read only part of the line at a time though (?). I only wish to read the first two variables, then the third, then the fourth etc, not all at once.
 
Why would you want to spend a lot of thought on how to read part of the record instead of just readong it completely and simply ignore the values that you do not need at the time ? And read the whole record next time when you need it and ignore other figures ?

I fear, I do not quite get what your problem is.

Norbert

The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
nick: you really need to get out of your box and stop making up extra constraints...

...as far as we can tell, there is no reason why you can't read the entire line in one shot...and wait until you need to use the values or never use them if you don't need them...








 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top