读取未知行数

发布于 2024-12-27 09:21:01 字数 102 浏览 4 评论 0原文

我需要用 C 语言实现该程序,该程序从标准输入读取未知数量的行。 我知道最大行数是100。 我尝试使用 gets ,但我不知道何时停止循环。 你能告诉我如何实施吗?

I need to implement in C the program ,which reads an unknown number of lines from stdin.
I know that the maximum number of lines is 100.
I tried to use gets ,but I don`t know when to stop the loop.
Can you advise me how to implement it?

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

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

发布评论

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

评论(1

溺ぐ爱和你が 2025-01-03 09:21:01

这取决于您希望程序何时停止。有几种常见的方法:

  1. 从不:运行无限循环,直到最终用户点击 ^C 或使用操作系统的功能终止程序
  2. 直到用户输入特殊标记,即“关键字”QUITEXIT 等单独一行
  3. ,直到用户输入空行(即按 Enter

为止在你的情况下是100,你可以用它作为限制一旦达到最大值,自动终止输入。

This depends on when you want your program to stop. There are several common approaches:

  1. Never: you run an infinite loop until end-user hits ^C or otherwise terminates your program using the facilities of your operating system
  2. Until the user enters a special marker, i.e. a "keyword" QUIT, EXIT, etc. on a line by itself
  3. Until the user enters an empty line (i.e. hits Enter)

Since the max number in your case is 100, you can use it as the limit to automatically terminate the input once the max is reached.

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