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!

Reading a reading a column 1

Status
Not open for further replies.

niloy112

Programmer
Mar 5, 2013
7
CA
Is it possible to read a particular column in a file by using fortran? Say i want to read the 5th column of a file. Is it possible to do so?As an example?

This is a data from a file.
asd1 rty2 iii1
uhy2 koi3 kjh4
kjh3 jhg3 kjh5

I want to make three file from these data considering there fourth column value data(1,2,3). The resultant 3 file will have the following data
asd1 rty2 iii1 is first one, uhy2 koi3 kjh4 in second one and kjh3 jhg3 kjh5 will be on third one.
 
Yes. it is. You can read data to memory and then save them to files any way you want. But if you are not familiar as yet with one of the very basic concepts of fortran, you should think twice if you want to start on this path.

I must say, I do not quite understand what you want to achieve.
This prog would read a file with three columns of data and creates new files for every new line of your old file, up to 999 lines that is.

Code:
program SplitLine          ! any name will do
implicit none              ! allways
character*4 c1,c2,c3       ! the variables used to store your data in memory
character*10 filname       ! this will hold the name your new files
integer iFile              ! running number of your new files
integer iEnd               ! indicating end of old file 

open (unit = 10, file = 'oldfile.txt')                ! open your original file
iFile = 0                                             ! set strating value
iEnd = 0
do while (iend .eq. 0)                                ! start loop´and run it for as long as there are data to read in your file
    iFile = ifile + 1
    write (filname, '(''new'',i3.3,'.txt')  iFile     ! build a new file for each line of your old file
    open (unit = 11, file = filename)
    read (10, *, iostat = iend) c1, c2, c3            ! read data from your old file
    write (11, *) c1, c2, c3                          ! write them to your new files
    close (unit = 11)
enddo
end program

Norbert




The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Try reading with a format of '(3X,I1)'
 
Dear Norbert,
Thank you for your mail.
Thank you for your codes also.
Can i explain my problem in detail?

--
I have a file in which there are a lot of rows and 80 column.
I will have to split the whole file into 4 pieces.

There are four symbol on the 10 th column of the file (like a,b,c,d).
I will have to split the whole file into four different file.I am giving an example.Look at the alphabet in capital format(A,B,C,D)

q11234567A728
hfudshfjkAsaj
fjfdjhfjdAjshfa
sldjfhskjBfk
sdahfkajsBhf
kjsdhfkajB
kjsdfhasjB
kjsahdfkjC
hjasbdfakCsh
sadbfajshCsadh
ksjadhfakCsd
kjsdahfkjCqweqw
kashdfakjCqweq
saldfjsdjCwe
dqweqweqwDuq

i need to make four files (there name may be A,B,C,D) from this file say..
q11234567A728
hfudshfjkAsaj
fjfdjhfjdAjshfa

sldjfhskjBfk
sdahfkajsBhf
kjsdhfkajB
kjsdfhasjB

kjsahdfkjC
hjasbdfakCsh
sadbfajshCsadh
ksjadhfakCsd
kjsdahfkjCqweqw
kashdfakjCqweq
saldfjsdjCwe

and

dqweqweqwDuq

After getting these file i will be able to use one of these files, say the file contain 'C', into another work.
Thank you for your help once again.
I am also requesting you to provide me some link or reference from where i can get some more information regarding these things.

Niloy



 
I think now I got it.

I would propose a code like this:

Code:
program SplitFile
implicit none
character*80 cString
integer iEnd, iUnit

open (unit = 10, file = 'Oldfile.txt')          ´! open files and set starting values
open (unit = 11, file = 'NewAFile.txt')
open (unit = 12, file = 'NewBFile.txt')
open (unit = 13, file = 'NewCFile.txt')
open (unit = 14, file = 'NewDFile.txt')
iEnd = 0

do whilöe (iEnd .eq.0)
    read (1ß, '(a80)', iostat = iEnd) cString    ! read complete line 
    select case (cString (10:10))                ! establish the file this is to be written to by the tenth character of cString
        case ('a')
            iUnit = 11
        case ('b')
            iUnit = 12
        case ('c')
            iUnit = 13
        case ('d')
            iUnit = 14
    end select
    write (iUnit, '(a89)') cString                ! write the string to the appropriate file
enddo
close (unit = 10)                                 ! and clean up       
close (unit = 11)        
close (unit = 12)        
close (unit = 13)        
close (unit = 14)
end program

This code should sort the contents of your original file for the 10th character and store the string into four different files.
I am a little in a hurry, so I did not test this, but I'd say, it is pretty easy to understand how this code is intended to work.


Norbert





The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Dear Norbert,

Thank you very very much for your reply.
I was a great help for me.
Can you suggest me some tips or references for understanding string very clearly.

Niloy
 
Well, niloy, if you want to continue with fortran, if this one wasn't meant to be a one time event, then you should try to learn fortran from the beginning. I can recommend the books of Stephen F. Chapman titled 'Fortran xxxx for Scientists and Engineers' with xxxx being the fortran version (90/95 or such). There everything is explained in great detail with very illuminating programming examples.

Norbert

The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top