fortran 데이터 읽는 방법에 대해서 좀 여쭙겠습니다.

nopane11의 이미지

160|19970101|5.3|13.4|-5.2|304.9|31.1|0|
160|19970102||0|-6.5|337.4|24.7|0|
160|19970103|-4.3|.3|-7.7|334.4|23.1|0|

이런 형식의 데이터가 있을때 |가 문제거든요..
이 형식의 데이터가 aaa.dat로 저장 되있다고 할때
저 데이터를 하나의 문자로 처음에 읽어서

character(len=80) :: dat
character :: k='|'
character(len=4) :: kp=' '

open(1,file='aaa.dat',status='old')

1 read(1,'a80',end=11) dat

do i=1,80
if(dat(i:i) == '|') k=kp (이부분이 문제인거같은데.. |문자를 공백으로 치환하는 부분)
enddo

write(*,*) dat
goto 1
11 continue

end
이런 프로그램을 만들어보려고 합니다.
|가 공백으로 바뀌질 않더군요..

어떤 데이터가 있을때 그걸 전부 하나의 character로 읽고 그 하나의 character에서 특정한 문자만 치환하는 그런 방법을 알고계시면
부탁드립니다.

yeonpil_net의 이미지

! 표준입출력 유닛이 5, 6, 0으로 가정(뭐 거의 그렇습니다.)
! 한 라인 255 가정입니다. 필요하면 바꾸세요.
 
program REMOVE_DELIM
 implicit none
!----------------------------------------------------------------------
 character(len=255) :: in_file, out_file
 character(len=255) :: line
 integer, parameter :: STDIN=5, STDOUT=6, STDERR=0
 integer :: unit_in, unit_out
 integer :: io_status
 integer :: i, j, line_len, line_number
!----------------------------------------------------------------------
 line_number = 0
 io_status = -1
 unit_in  = 10
 unit_out = 11
 
!----------------------------------------------------------------------
! Read file name && Open
!----------------------------------------------------------------------
 write (stdout, fmt='(a)', advance='no') 'Input File Name (In) : '
 read  (STDIN, '(a)') in_file
 in_file = trim(in_file)
 open (unit_in, file=in_file, iostat=io_status, status='old')
 if (io_status /= 0) then
     write (STDOUT, *) 'File Open Error'
     stop
 end if
 
 write (stdout, fmt='(a)', advance='no') 'Input File Name (OUT) : '
 read  (STDIN, '(a)') out_file
 out_file = trim(out_file)
 open (unit_out, file=out_file, iostat=io_status, status='new')
 if (io_status /= 0) then
     write (STDOUT, *) 'File Open Error'
     stop
 end if
 
!----------------------------------------------------------------------
! Read Lines
!----------------------------------------------------------------------
 do
     read (unit_in, '(a)', iostat=io_status) line
     if (io_status /= 0) then
         if (io_status < 0) then
             write (STDOUT, *) 'End : File Converted'
             exit
         else
             write (STDOUT, *) 'Read Error Occured'
             write (STDERR, *) 'Read Error : ', io_status
             exit
         end if
     end if
 
     line_number = line_number + 1
     line_len = len_trim(line)
 
     if (line_len > 255) then
         write (STDOUT, '(a)') 'In file (', in_file ,'):(',line_number,') line : A Line length must be smaller than 255'
         stop
     end if
 
     do i = 1, line_len
         if (line(i:i) == '|') then
             if (line(i+1:i+1) == '|') then
                 write (unit_out, '(a)', advance='no') ' 0'
             else
                 write (unit_out, '(a)', advance='no') ' '
             end if
         else
             write(unit_out, '(a)', advance='no') line(i:i)
         end if
     end do
     write(unit_out, *)
 end do
 
 close(unit_in)
 close(unit_out)
end program

!23456---1----+----2----+----3----+----4----+----5----+----6----+----7-2--+----8
"배웠다"는 "할 수 있다"의 동의어가 아니다.

nopane11의 이미지

거듭 감사드립니다.
한번도 못 본 새로운 표현이네요.. 역시 세상은 넓고 고수는 많군요
친절하고 자세한 설명 감사드립니다.
헌데 표준 입출력유닛이 5,6,0은 무얼 말하는 거죠? 제가 말에 상당히 약해서..
line_len = len_trim(line)의 역할과
advance='no'와 iostat=io_status 란 표현을 처음 접해보는데 조금만 더 설명해주시면 정말 감사드립니다

yeonpil_net의 이미지

질문자 코드에 보이는 스타일을 지적하자면,

character에 len 지시자를 쓰고, :: 같은 녀석을 쓰면서 90이상을 지향하는데..

문번 continue 와 같은 올드하고 이젠 버려야할 스타일로..지양되는 것들을 사용하는.. 짬뽕 개념을 가지고 있는 듯 하시고요.

goto .. contiune 같은 것들은 버리세요.

전체적으로 기본적인 문법뿐이라..

len_trim 은 내장함수로 뒷 공백을 제거한 문자열을 돌려줍니다.
advance 속성은 출력을 다음 줄로 할지 말 지 제어할 수 있습니다. 기본이 yes고 같은 줄에 연속으로 출력하기 위해 no를 씁니다.
iostat은 end='문번' 을 쓰는 것보다 훨씬 나은 방법입니다.

저는 문번이 나오는 것은 format문 빼곤 욕하면서 보는 체질이라.

stdin, stdout, stderr에 대한 유닛은 대부분의 환경(포트란컴파일러 + 플랫폼) 에서 5, 6, 0 입니다. C에선 0,1,2 로 번호가 매겨져있죠.
write에서 * 대신 6
read 에서 * 대신 5 를 써도 무방해집니다. (완전히 그러하다곤 볼 수 없습니다. 실질적으로 그런거지 표준사항은 아닙니다.)

stdin, stdout만 쓸 거였음 * 로해서, 질문자 안 헷갈리게 할 수 있을 것을.. 에러처리를 따로 표준에러로 출력한답시고 굳이 구별하려고 코딩이 늘었습니다.

!23456---1----+----2----+----3----+----4----+----5----+----6----+----7-2--+----8
"배웠다"는 "할 수 있다"의 동의어가 아니다.

nopane11의 이미지

질문한 스타일의 프로그램은 평소에도 저렇게 하는데... 이게 상당히 구식이었군요..
친절한 설명 감사드립니다. 덕분에 새로운걸 배워갑니다

yeonpil_net의 이미지


문자열 처리를 쉽게 할 수 있는 것도 아닌데..

저런 전처리 개념의 프로그래밍은 그냥 스크립트형 언어들 예를들어 awk같은 것으로 하면, 몇줄 필요하지도 않고 가볍게 짤 수 있으니..

포트란을 선택하는 것은 별로죠.

!23456---1----+----2----+----3----+----4----+----5----+----6----+----7-2--+----8
"배웠다"는 "할 수 있다"의 동의어가 아니다.

nopane11의 이미지

아직 수준이 낮아서 선배들 말로는 포트란만 완벽히 익히면 다른 프로그램은 배우기 쉽다는 말도 들었거니와
아직 다른 프로그램에 손댈 여력이 없네요..
포트란 익히는게 상당히 힘드네요...
연필님의 블로그? 개인 홈페이지?를 허락없이 들어가서 너무 좋길래 회원가입했는데..
비쥬얼 포트란 메뉴얼 한글화는 어떻게해야 볼 수 있는건가요?

yeonpil_net의 이미지

생각없이.. STDOUT, STDERR 자동완성 바뀐것도 안보고 그냥 날림으로 타이핑하다..
쯪쯪 ㅠ.ㅠ

!23456---1----+----2----+----3----+----4----+----5----+----6----+----7-2--+----8
"배웠다"는 "할 수 있다"의 동의어가 아니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.