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

Read Entire line regardless of content/delimiters

Status
Not open for further replies.

jwarnett

Programmer
Feb 3, 2012
1
GB
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
 
This program copies the input file to the output file:
read_write.f95
Code:
[COLOR=#a020f0]program[/color] read_write
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]

  [COLOR=#0000ff]! Variables[/color]
  [COLOR=#2e8b57][b]integer[/b][/color] stat
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]80[/color]) :: line

  [COLOR=#0000ff]! open input file[/color]
  [COLOR=#804040][b]open[/b][/color] ([COLOR=#ff00ff]10[/color], [COLOR=#804040][b]file[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'inp_file.txt'[/color], [COLOR=#804040][b]status[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'old'[/color], [COLOR=#804040][b]iostat[/b][/color][COLOR=#804040][b]=[/b][/color]stat)
  [COLOR=#804040][b]if[/b][/color] (stat [COLOR=#804040][b].ne.[/b][/color] [COLOR=#ff00ff]0[/color])[COLOR=#804040][b]then[/b][/color]
    [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'inp_file can not be opened'[/color]
    [COLOR=#804040][b]go to[/b][/color] [COLOR=#ff00ff]99[/color]
  [COLOR=#804040][b]end if[/b][/color]

  [COLOR=#0000ff]! open output file[/color]
  [COLOR=#804040][b]open[/b][/color] ([COLOR=#ff00ff]20[/color], [COLOR=#804040][b]file[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'out_file.txt'[/color], [COLOR=#804040][b]status[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'unknown'[/color], [COLOR=#804040][b]iostat[/b][/color][COLOR=#804040][b]=[/b][/color]stat)
  [COLOR=#804040][b]if[/b][/color] (stat [COLOR=#804040][b].ne.[/b][/color] [COLOR=#ff00ff]0[/color])[COLOR=#804040][b]then[/b][/color]
    [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'out_file can not be opened'[/color]
    [COLOR=#804040][b]go to[/b][/color] [COLOR=#ff00ff]99[/color]
  [COLOR=#804040][b]end if[/b][/color]

  [COLOR=#0000ff]! process file[/color]
  [COLOR=#804040][b]do[/b][/color] [COLOR=#804040][b]while[/b][/color] ([COLOR=#ff00ff].true.[/color])
    [COLOR=#804040][b]read[/b][/color] ([COLOR=#ff00ff]10[/color], [COLOR=#ff00ff]'(A)'[/color], [COLOR=#804040][b]end[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]99[/b][/color]) line [COLOR=#0000ff]! read line from input file[/color]
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color], [COLOR=#ff00ff]'(80A)'[/color]) line        [COLOR=#0000ff]! write line to the screen[/color]
    [COLOR=#804040][b]write[/b][/color]([COLOR=#ff00ff]20[/color], [COLOR=#ff00ff]'(A)'[/color]) [COLOR=#008080]trim[/color](line)   [COLOR=#0000ff]! write line to output file[/color]
  [COLOR=#804040][b]enddo[/b][/color]
  
  [COLOR=#0000ff]! close files[/color]
[COLOR=#804040][b]  99 continue[/b][/color]
  [COLOR=#804040][b]close[/b][/color] ([COLOR=#ff00ff]10[/color])
  [COLOR=#804040][b]close[/b][/color] ([COLOR=#ff00ff]20[/color])
    
[COLOR=#a020f0]end program[/color] 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:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top