如何创建 Linux 等效的“nl” C 中使用系统调用的命令?

发布于 2024-07-16 12:11:19 字数 1459 浏览 5 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

缺⑴份安定 2024-07-23 12:11:20

如果您事先不知道行的长度,则没有找到 \n 位置的捷径。 也就是说,除非您使用易于实现的函数来分割线,但这可能只是在某处做同样的事情。

也就是说,是的,您必须循环遍历字符:)

If you don't know the lengths of the lines in advance, there is no shortcut for finding the place of the \n. That is, unless you use a readily implemented function for splitting the lines, but that would probably just do the same thing somewhere down the line.

That is, yes, you have to loop through the chars :)

奢华的一滴泪 2024-07-23 12:11:20

一个非常粗略的轮廓:

  • 您可以将 getc(file) 替换为 read(file,&c,1):只需编写一个将文件接受为一个参数并返回一个字符。 将字符读入本地变量并返回。

  • 然后实现一个getline(file):创建一个缓冲区,开始一次一个地向其中读取字符,当遇到'\n'时停止(什么如果你的空间用完了,你会怎么做?),并返回一个指向缓冲区的指针(如何分配缓冲区以使其工作?)。

  • 使用 getline 一次读取一行,然后写入行号和行。

  • 循环直到文件用完...


减少系统调用:

一次性将整个文件读入一个大缓冲区——这非常简单如果您有文件大小——并一次遍历一个字符以找到行的开头和结尾(使用两个指针)。

A very rough outline:

  • You can replace getc(file) with read(file,&c,1): just write a function which accepts the file as an argument and return a character. Read the character into a local ariable and return it.

  • Then implement a getline(file): Make a buffer, starting reading characters into it one at a time, stop when you come to '\n' (what will you do if you run out of room?), and return a pointer to the buffer (How can you allocate the buffer for this to work?).

  • Use you getline to read one line at a time, then write the line number and the line.

  • Loop until you run out of file...


To make fewer system calls:

Read the whole file into a large buffer in one go---that's pretty easy if you have the file size---and walk it one character at a time to find the line starts and ends (use two pointers).

不离久伴 2024-07-23 12:11:20

您实际上不需要知道 '\n' 的位置来对行进行编号。 您只需打印出行号(从 1 开始),然后读取字符,直到遇到换行符 '\n'。 使用 write() 将每个字符复制回 stdout,如果该字符是换行符,则增加当前行数并将其打印出来。

You don't actually need to know the location of the '\n' to number the lines. You just print out the line number (starting with 1), and read characters until you hit a newline '\n'. Copy each character back to stdout with write(), and if the character is a newline, increment the count of current lines and print that out, too.

書生途 2024-07-23 12:11:19

您必须在缓冲区中搜索 \n,没有办法绕过它。 不用担心性能,程序大部分时间都花在readwrite上。

另外,请确保缓冲区足够大。 不要调用 read(file,&c,1) 因为它会非常慢。

You will have to search the buffer for \n, there is no way around it. Don't worry about the performance, the progam will spend most of the time in read and write.

Also, make sure you make the buffer sufficiently large. Don't call read(file,&c,1) as it will be incredibly slow.

风铃鹿 2024-07-23 12:11:19

找到“\n”的唯一方法是搜索它。 如果您无法使用 strchr(3),则必须迭代缓冲区以找到您要查找的内容(阅读:实现它)。

The only way to find a '\n' is to search for it. If you can't use strchr(3), you've got to iterate over the buffer to find what you're looking for (read: implement it).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文