×
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

Read Entire line regardless of content/delimiters

Read Entire line regardless of content/delimiters

Read Entire line regardless of content/delimiters

(OP)
Hey,

Basically I am trying to split a file up which has things of the format

ITEM: TIMESTEP
32
ITEM: ATOMS
100
ITEM: BOX BOUNDS
0 0
0 -1
0 1
ITEM id type x y z
1 1 0 0 0.1
2 1 0 -0.1 0.2
....

And then repeats with similar headers and content.

So my fortran file was reading this in and was writing line by line to a new text file until it had read 100 items (for example) as shown by 100 atoms, and then start a new file.

I was using

READ(10,*) lineread
WRITE(unitwr,*) lineread

where lineread is a character variable of length 100. Now this was cutting things out as it was taking spaces as delimiters. So the entire line was not being read and written, just up to the first delimiter. So instead I tried

READ(10,'(A)') lineread
WRITE(unitwr,'(A)') lineread

But for some reason it wrote everything after a couple of runs, instead of the number of lines. Its like it read onto the next line and copied that too.

So is there a way to do an entire line read regardless of delimiters? I can't seem to find it.
Or would it be better to keep reading until it encounters ITEM: TIMESTEP, but again, got the problem with the delimiter.

Cheers

Jay
 

RE: Read Entire line regardless of content/delimiters

This program copies the input file to the output file:
read_write.f95

CODE

program read_write
  implicit none

  ! Variables
  integer stat
  character(80) :: line

  ! open input file
  open (10, file='inp_file.txt', status='old', iostat=stat)
  if (stat .ne. 0)then
    write (*,*) 'inp_file can not be opened'
    go to 99
  end if

  ! open output file
  open (20, file='out_file.txt', status='unknown', iostat=stat)
  if (stat .ne. 0)then
    write (*,*) 'out_file can not be opened'
    go to 99
  end if

  ! process file
  do while (.true.)
    read (10, '(A)', end=99) line ! read line from input file
    write(*, '(80A)') line        ! write line to the screen
    write(20, '(A)') trim(line)   ! write line to output file
  enddo
  
  ! close files
  99 continue
  close (10)
  close (20)
    
end program read_write
So when we have a text file
inp_file.txt

CODE

ITEM: TIMESTEP
32
ITEM: ATOMS
100
ITEM: BOX BOUNDS
0 0
0 -1
0 1
ITEM id type x y z
1 1 0 0 0.1
2 1 0 -0.1 0.2
then the program reads it line by line, writes every line to the screen and to the file out_file.txt

CODE

$ g95 read_write.f95 -o read_write

$ read_write
ITEM: TIMESTEP
32
ITEM: ATOMS
100
ITEM: BOX BOUNDS
0 0
0 -1
0 1
ITEM id type x y z
1 1 0 0 0.1
2 1 0 -0.1 0.2

If you want to extract parameters from your input file, then maybe these older threads could help you:
http://www.tek-tips.com/viewthread.cfm?qid=1662044
http://www.tek-tips.com/viewthread.cfm?qid=1602458  

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