有没有办法找出当前行的行号从文件中读取? [英] Is there a way to find the line number of the current line being read from a file?

查看:132
本文介绍了有没有办法找出当前行的行号从文件中读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)是否有在C的方式,我们可以发现,我们从文件中读取行的行号。

2)我也想知道是否有另一种方式来找出行的文件比创建一个循环查找 EOF 其他总数在每行,直到它到达终点。


解决方案

  

1)是否有在C的方式,我们可以发现,我们从文件中读取行的行号。


除非你计数从文件的开始处的线。你不能只将自己定位在任意一个文件,并知道你在哪一行文字,除非有文件本身的信息告诉你,或者你已经由previous创建各种各样的指数越过文件。通常的做法是,你处理从文件的行积累了行计数。


  

2)我也想知道是否有另一种方法来找出线的总数在其他的文件不是通过创建一个循环,看起来对EOF中的每一行,直至到达结束


没有。你必须通过整个运行文件和计数行数。任何外部工具(如厕所)正在做的正是这一点。你需要知道不同的行结束样式。根据您所使用阅读文件的功能是什么,你可能会或可能不会有自动换行转换。

1) Is there a way in C that we could find the line number of a line that we are reading from a file.

2) I would also like to know if there is another way to find out the total number of lines in a file other than by creating a loop which looks for EOF in each line until it reaches the end.

解决方案

1)Is there a way in C that we could find the line number of a line that we are reading from a file.

Not unless you count the lines from the beginning of the file. You can't just position yourself arbitrarily in a file and know what line of text you're on, unless there is information in the file itself telling you, or you have created an index of sorts by a previous pass over the file. The usual approach is to accumulate a line count as you process lines from the file.

2)I would also like to know if there is another way to find out the total number of lines in a file other than by creating a loop which looks for EOF in each line until it reaches the end.

No. You must run through the whole the file and count the number of lines. Any external tools (such as wc) are doing exactly this. You need to be aware of different line-ending styles. Depending on what functions you are using to read the file, you may or may not have automatic newline translation.

这篇关于有没有办法找出当前行的行号从文件中读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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