reading lines with trailing white spaces
- From: Kamaraju S Kusumanchi <kamaraju@xxxxxxxxxxxxxx>
- Date: Thu, 22 Feb 2007 02:07:27 -0500
I am having very basic problem with reading lines in a file with trailing
white spaces. Consider
program copy_a_file
! Fortran 90 program to copy a file
implicit none
integer :: ios
integer, parameter :: file_len=20, line_len=200
character(len=file_len) :: filename1, filename2
character(len=line_len) :: line
write(filename1, '(A)') 'copy_a_file.f90'
write(filename2, '(A)') 'temp.txt'
write(*,*) '******************************'
write(*,*) 'cp ', filename1, ' ', filename2
write(*,*) '******************************'
open(unit=101, file=filename1, action='read')
open(unit=102, file=filename2, action='write')
do
! read a line from file
read(101,'(A)', IOSTAT=ios) line
! negative IOSTAT indicates that an "end of file" or "end of record"
! condition occurred
if (ios < 0) then
exit
else
write(102,'(A)') trim(line)
end if
end do
close(unit=101)
close(unit=102)
end program copy_a_file
The strategy I am adopting is to read a line into chracter array of size 200
and then trim it and write it into a new file. Because of the trim function
I will loose all the trailing spaces in the original file. If I dont use
the trim function, I will end up with lines of length 200 (filled with
spaces at the end) even if the lines in the original file are shorter than
200 characters width. I want to have an exact copy of the original file and
I want to do this in Fortran (since the actual problem is somewhat
different)...
My questions are
1) Is there a clever way of copying a file into another file even if has
some trailing white space characters?
2) Is there a clever way of reading lines with trailing white spaces and
preserving them?
thanks
raju
--
Kamaraju S Kusumanchi
http://www.people.cornell.edu/pages/kk288/
http://malayamaarutham.blogspot.com/
.
- Follow-Ups:
- Re: reading lines with trailing white spaces
- From: Paul van Delst
- RE: reading lines with trailing white spaces
- From: meek
- Re: reading lines with trailing white spaces
- From: Arjen Markus
- Re: reading lines with trailing white spaces
- Prev by Date: Re: what to buy now?
- Next by Date: Re: reading lines with trailing white spaces
- Previous by thread: what to buy now?
- Next by thread: Re: reading lines with trailing white spaces
- Index(es):
Relevant Pages
|