×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Reading character up to ";" from a csv-file

Reading character up to ";" from a csv-file

Reading character up to ";" from a csv-file

(OP)
Here I have the following csv-file:

CODE -->

test_001.txt;====== Def. 1 ======;====== ;Definition 1; ======
bedarf_0002.txt;====== ef ======;====== ;ef; ======
bedarf_0003.txt;====== Definition 3 ======;====== ;Definition 3; ======
bedarf_04.txt;====== Definition 4 ======;====== ;Definition 4; ======
bedarf_0005.txt;====== Test ======;====== ;Test; ======
bedarf_0006.txt;====== Definition 6 ======;====== ;Definition 6; ======
bedarf_0007.txt;====== Definition 7 ======;====== ;Definition 7; ======
bedarf_0008.txt;====== Definition 8 ======;====== ;Definition 8; ======
bedarf_0009.txt;====== Definition 9 ======;====== ;Definition 9; ====== 
and the following short Fortran program to handle "a problem":

CODE --> Fortran

program doku
      character dn*16, di*26
      open (3,file = 'datei.csv', status = 'old')
  100 read (3,'(a15,1x,a)',end=1000) dn,di
      open (4,file = dn,status='replace')
      write (4,*) di
      close(4)
      goto 100
 1000 end 

I would like to achieve that variable dn will be filled with f.e. "test_001.txt" and di will be filled with f.e. "====== Def. 1 ======". In other words, the variables should be read up to an without the semikolon.
What do I need to do to achieve that.

RE: Reading character up to ";" from a csv-file

Hi virk24,

you can try something like this:

virk24.f95

CODE

program virk24
  implicit none

  integer stat, line_nr, separator_pos, beg_idx 
  character :: separator = ';'
  character(80) :: line
  character(16) :: dn
  character(26) :: di

  ! open file
  open (1, file='virk24.csv', status='old', iostat=stat)
  if (stat .ne. 0) then
    write(*,*) 'File cannot be opened !'
    go to 99
  end if

  write(*,*) 'Reading CSV-file...' 
  ! process file
  line_nr = 0
  do while (.true.) 
    read(1, '(A)', end=99) line
    line_nr = line_nr + 1
    
    ! extract columns
    ! 1. column
    beg_idx = 1
    separator_pos = index(line, separator)
    dn = line(beg_idx: separator_pos-1)
    ! 2. column
    beg_idx = separator_pos + 1
    separator_pos = index(line(beg_idx:), separator) 
    di = line(beg_idx: beg_idx + separator_pos - 2)
    ! result
    write(*,*) trim(dn),' ',trim(di)
  end do

  ! close file
  99 continue
  close(1)

  write(*,*) 'Line processed:', line_nr

  write(*,*) 'Done.'
end program virk24 

Output:

CODE

$ gfortran virk24.f95 -o virk24
$ ./virk24
 Reading CSV-file...
 test_001.txt ====== Def. 1 ======
 bedarf_0002.txt ====== ef ======
 bedarf_0003.txt ====== Definition 3 ======
 bedarf_04.txt ====== Definition 4 ======
 bedarf_0005.txt ====== Test ======
 bedarf_0006.txt ====== Definition 6 ======
 bedarf_0007.txt ====== Definition 7 ======
 bedarf_0008.txt ====== Definition 8 ======
 bedarf_0009.txt ====== Definition 9 ======
 Line processed:           9
 Done. 

RE: Reading character up to ";" from a csv-file

Did it help you or not ?

RE: Reading character up to ";" from a csv-file

(OP)
Thank you, mikrom! I think your code was helpful. I in between did the following:

CODE --> fortran

program doku
      character line*100
      open(3,file='datei.csv', status='old')
  100 read(3,'(a)',iostat=n) line
      if (n < 0) goto 1000
      i = index(line,';')
      j = index(line(i+1:),';')
      open (4,file = line(1:i-1)//'.txt',status='replace')
      write(4,*) '===== ',trim(line(i+1:i+j-1)),' ====='
      close(4)
      goto 100
 1000 end 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close