文本文件的行数 [英] Number of lines of a text file

查看:168
本文介绍了文本文件的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个接受文件名的函数(即data.txt)并产生该文件的行数。

  data.txt:
24 42
45 54
67 76
89 98
12 21
99 99
33 33

下面这段代码是我尝试构建一个函数,文件名data.txt并产生文件的行数。代码的第一部分(第1-12行)定义了函数,第二部分(第14-19行)是我在程序中调用函数的地方。下面代码的输出是1比7更多(look up - data.txt有7行)。

pre $ function count_lines(文件名)结果(nlines)
字符::文件名
整数:: nlines
打开(10,文件=文件名)
nlines = 0
do
(10,*,iostat = io)
nlines = nlines + 1
if(io / = 0)exit
end do
close(10)
end函数count_lines

程序myread
integer :: count_lines

print *,count_lines(data.txt)

结束程序myread

尝试调试:



我认为它与do循环不起作用有关,但我不确定。如果(io> 0),我已将if语句更改为,并且这给出了 2 的输出,不明白。目前,我在编译错误输出时没有收到错误输出。 (伪参数)只有一个字符长。如果你提供更长的一个,可能会发生任何事情(通常它是封顶的,尽管如此)。
这并不会导致错误,因为您没有指定文件的 status ,甚至检查操作是否成功(实际上,您刚创建一个新文件 d )。
您可以通过使用假定的长度字符串来避免这种情况:



  function count_lines文件名)结果(nlines)
字符(len = *)::文件名
! ...

open(10,file = filename,iostat = io,status ='old')
if(io / = 0)stop'无法打开文件! '





 函数count_lines(文件名)结果(nlines)
隐含无
字符(len = *)::文件名
整数:: nlines
整数:: io

打开(10,file = filename,iostat = io,status ='old')
if(io / = 0)stop'无法打开文件! '

nlines = 0
do
read(10,*,iostat = io)
if(io / = 0)exit
nlines = nlines + 1
end do
close(10)
end function count_lines

[在之后增加以确保行数正确。]




问题的第二部分:
$ b

正面(非零)错误代码对应于除 IOSTAT_END 以外的任何错误。 (文件结束)或 IOSTAT_EOR (结束记录)。在循环的第一轮( io == IOSTAT_END ,我使用我的编译器进行了检查)中读入(新)文件后,您正在尝试读取超出结尾的内容。这导致了一个积极的错误。由于在之前发生了增量,所以您退出循环,最终值为 2

I'm trying to make a function that takes in a file name (i.e. "data.txt") and produces the number of lines of that file.

data.txt :
24 42
45 54
67 76
89 98
12 21
99 99
33 33

The below piece of code is my attempt to build a function that takes in the filename "data.txt" and produces the number of lines of the file. The first part (lines 1-12) of the code is defining the function and the second part (lines 14-19) is where I call the function in a program. The output of the below code is 1 rarther than 7 (look up - data.txt has 7 lines).

function count_lines(filename) result(nlines)
  character :: filename
  integer :: nlines 
  open(10,file=filename)
  nlines = 0
  do
  read(10,*,iostat=io)
  nlines = nlines + 1
  if (io/=0) exit
  end do
  close(10)
end function count_lines

program myread 
  integer :: count_lines

  print *, count_lines("data.txt")

end program myread

Attempt a debuging:

I think it has something to do with the do loop not working, but I'm unsure. I have changed the if statement to if (io>0) and this gives an output of 2, which I don't understand. At present I'm not getting an error outputs when I compile just the wrong output.

解决方案

filename (the dummy argument) is only one character long. Anything might happen if you provide a longer one (usually it is capped, though). This does not result in an error as you did not specify the status of the file, or even checked whether the operation was successful (In fact, you just created a new file d). You can avoid this by using an assumed length string:

function count_lines(filename) result(nlines)
  character(len=*) :: filename
  ! ...

  open(10,file=filename, iostat=io, status='old')
  if (io/=0) stop 'Cannot open file! '

function count_lines(filename) result(nlines)
  implicit none
  character(len=*)    :: filename
  integer             :: nlines 
  integer             :: io

  open(10,file=filename, iostat=io, status='old')
  if (io/=0) stop 'Cannot open file! '

  nlines = 0
  do
    read(10,*,iostat=io)
    if (io/=0) exit
    nlines = nlines + 1
  end do
  close(10)
end function count_lines

[Increment after the check to ensure a correct line count.]


For the second part of your question:

Positive (non-zero) error-codes correspond to any error other than IOSTAT_END (end-of-file) or IOSTAT_EOR (end-of-record). After reading in the (new) file in the first round of the loop (io==IOSTAT_END, I checked with my compiler), you are trying to read beyond the end... This results in a positive error. Since the increment occurs before you exit the loop, the final value is 2.

这篇关于文本文件的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆