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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

virk24

Technical User
Apr 6, 2023
4
0
0
NL
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:
      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.
 
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.

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

Code:
      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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top