读取文件并根据另一个文件更改编号
我知道如何更改一个文件中文本中的数字,但我需要使用这样的表格(假设 file=word
):
1 # K
2 # L
3 # M
并在另一个文件 text.dat
中:
1 1
1 2
1 3
在第二列中更改文本 K
、L< 中的数字
1
、2
、3
/code>, M
使用 Fortran 程序。我知道我可以使用数组,或者我可以像带索引的行一样读取它。有谁知道如何从不同文件中获取文本?我有问题如何将一个文件中不断变化的数字合并到使用两个文件的文本中。
我的起始代码:
program prevod
implicit none
integer :: i, k, maxgrps, a, p
character(LEN=40):: afile, line, lmpline
parameter (maxgrps=10)
character*10 :: atom, blank, atpname(maxgrps)
logical :: elements, first
afile="t2.dat"
open(20, file = "text.dat",status='old')
open(11,file="res",status='unknown')
do i=1,3
read(20,fmt='(a)') line
read(unit=line,fmt='(i2)') p
if(index(line,'element')==0) then
open(12,file=afile,status='old')
do a=1,3
read(unit=12,fmt='(a)') lmpline
k=index(lmpline,'#');if(k==0) goto 90
atom=lmpline(k:k+6)
k=len_trim(atom)
90 write(11,'(i2,a)') k, atom
enddo
write(11,'(i2,a)') atom
endif
close(12)
enddo
end program prevod
我想要的结果应该如下所示:
1 K
1 L
1 M
我需要从第二个文件将三个数字更改为 K、L、M。
I know how to change numbers in text in a one file, but I need to use table like this (lets say file=word
):
1 # K
2 # L
3 # M
and in another file text.dat
:
1 1
1 2
1 3
in the second column change the numbers 1
, 2
, 3
in text K
, L
, M
using Fortran program. I know I can use arrays or I can read it like a line with index. Does anyone has an idea how to do it taking the text from different file? I have problem how to combine changing numbers in one file to text employing two files.
My starting code:
program prevod
implicit none
integer :: i, k, maxgrps, a, p
character(LEN=40):: afile, line, lmpline
parameter (maxgrps=10)
character*10 :: atom, blank, atpname(maxgrps)
logical :: elements, first
afile="t2.dat"
open(20, file = "text.dat",status='old')
open(11,file="res",status='unknown')
do i=1,3
read(20,fmt='(a)') line
read(unit=line,fmt='(i2)') p
if(index(line,'element')==0) then
open(12,file=afile,status='old')
do a=1,3
read(unit=12,fmt='(a)') lmpline
k=index(lmpline,'#');if(k==0) goto 90
atom=lmpline(k:k+6)
k=len_trim(atom)
90 write(11,'(i2,a)') k, atom
enddo
write(11,'(i2,a)') atom
endif
close(12)
enddo
end program prevod
The result I want should look like:
1 K
1 L
1 M
I need to from second file this change of three numbers to K, L, M.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我们做任何其他事情之前,让我们摆脱那个
goto
。 什么都没有 好 可以来。这些行等效于
,但看起来您正在尝试进行错误处理,这可能应该是 接下来
要注意的是,在相同的代码中多次读取同一个文件通常不是一个好主意。相反,让我们将用于读取
word
的代码(我相信您在代码中称之为t2.dat
?)从读取text.dat< 的循环中取出/code>,并将每个
atom
存储在一个数组中,按照 Ian Bush 的评论:请注意,我已将最后一个
atom
替换为atoms(3)< /code> 当你在循环之外编写它时,所以它将取最终值。这可能不是您想要的。
现在我们可以查看读取
text.dat
和写入res
的循环。首先,您仅从text.dat
的每一行读取一个整数
。您可能想从每一行读取两个整数。此外,对于此类任务,通常最好使用列表定向读取而不是格式化读取,因为它们对文件格式更灵活。所以你的行将变成
现在你可以寻找与
p
匹配的atom
。您可以简单地以atoms(p)
的形式访问它,而不是滚动浏览文件并查找匹配项。所以你的代码将变成Before we do anything else, let's get rid of that
goto
. Nothing good can come of it. The linesare equivalent to
but it looks like you're trying to to error handling, which should probably instead be
The next thing to note is that it's not generally a good idea to read through the same file lots of times in the same code. Instead, let's pull your code for reading
word
(which I believe you callt2.dat
in your code?) out of the loop for readingtext.dat
, and store eachatom
in an array, as per Ian Bush's comments:Note that I've replaced the last
atom
withatoms(3)
as you're writing it outside the loop, so it will take the final value. This is probably not what you want.Now we can look at your loop for reading
text.dat
and writingres
. First off, you're only reading oneinteger
from each line oftext.dat
. You probably want to read both integers from each line. Also, it's generally better to use list-directed reads rather than formatted reads for this kind of task, as they are more flexible to file format. So your linewould become
Now you can look for the
atom
which matchesp
. Rather than scrolling through a file and finding the match, you can simply access this asatoms(p)
. And so your code would become