Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29250 Discussions

how to convert character data to integer data

lovemaths
Beginner
814 Views
hi,
i am new with programming. i have a problem on how to convert character data to integer. i wote the following:

character*10 Date
integer idy(1:31)
integer imth(1:12)
integer iyr(2008:2009)
open (1, file="/home/rf.csv")
do 10 i=1,361
read(Date,500) idy,imth,iyr
close(1)
open (1, file="/home/rf.csv")
write(1) idy,imth,iyr
10 print*, idy,imth,iyr
500 format (i2,1x,i2,1x,i4)
close(1)
end

when i run this program in the terminal, it says that there is an error in line 7. the csv file looks something like the following:

"31/12/2008",-999,-999,1.2,1.4,18.6,17.6,0.6,0.6,-999,-999,9.6,9.400001,-999,-999,1.8,1.8,1.6,1.8,2.2,2.4,-999,-999,0,0.2,1.2,1,-999,-999,,
"02/01/2009",-999,-999,0.0,0.0,0.0,0.0,0.6,0.6,-999,-999,0.0,0.0,-999,-999,65.6,57.0,21.8,21.2,0.2,0.0,-999,-999,0.2,0.2,0.4,0.2,-999,-999,,

i just copy the first two lines. what i want to do is to convert "31/12/2008" and the other dates to 31122008.

is there any suggestion?
0 Kudos
1 Reply
msbriggs
Beginner
814 Views
If I understand what you want to do, perhaps the following?

program date_xform

integer :: day, month, year
integer :: line

open (unit=1, file="rf.csv")

do line=1, 2
read (1, '( 1X, I2, 1X, I2, 1X, I4 )' ) day, month, year
write (*, '( I2.2, I2.2, I4.4 )' ) day, month, year
end do

close (1) ! optional in short program

end program date_xform




Some tips about your program: you have to read from the file using the unit number. If you want to do complicated processing, you can read the data into a string and then do additional reads from the string, but you need to do an initial read from the file. The notation "integer idy(1:31)" is making "idy" into an 1D array --having the days be an array by the possible values doesn't seem necessary for this problem. Opening and closing the same file for read and write will overwrite the data in the file. May I suggest learning Fortran 90/95 rather than FORTRAN 77?

If you want to alter the lines, changing just the date field and keeping the rest of the line, I'd open a second file to write the modified lines to. Read the each line into a long string and overwrite the first portion, the output the modified string to the second file.

0 Kudos
Reply